shiro 实现单用户登录,一个用户同一时刻只能在一个地方登录
如果我们跟shiro的源码,我们可以看到。当用户登录成功后,shiro会把用户名放到session的attribute中,key为DefaultSubjectContext_PRINCIPALS_SESSION_KEY,这个key的定义是在shiro的org.apache.shiro.subject.support.DefaultSubjectContext中,这个类有三个public的静态属性,其他都为private。其中PRINCIPALS_SESSION_KEY这个属性记录的是用户名,而AUTHENTICATED_SESSION_KEY属性记录的是用户认证,当用户登录成功后,这个attribute的值是true。
不过既然我们可以通过用户名可以找到用户对应的session,也很容易将该session删除,让用户重新建立一个新的sesison。这里给出一个帮助类,带有跳出用户的功能。
shiro.ini
[main] # Objects and their properties are defined here, # Such as the securityManager, Realms and anything # else needed to build the SecurityManager authc.loginUrl = /login.jsp authc.successUrl = /web/index.jsp #cache manager builtInCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager securityManager=org.apache.shiro.web.mgt.DefaultWebSecurityManager securityManager.cacheManager = $builtInCacheManager securityManager.sessionManager=$sessionManager #session 必须配置session,强制退出时,通过将session移除实现 sessionManager=org.apache.shiro.web.session.mgt.DefaultWebSessionManager sessionManager.sessionDAO=$sessionDAO sessionDAO=org.apache.shiro.session.mgt.eis.MemorySessionDAO # Create ldap realm ldapRealm = org.apache.shiro.realm.ldap.JndiLdapRealm #...... # Configure JDBC realm datasource dataSource = org.postgresql.ds.PGPoolingDataSource #....... # Create JDBC realm. jdbcRealm.permissionsLookupEnabled = true jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm jdbcRealm.userRolesQuery = ...... jdbcRealm.permissionsQuery = ...... jdbcRealm.dataSource = $dataSource #self realm localAuthorizingRealm = com.redbudtek.shiro.LocalAuthorizingRealm securityManager.realms = $ldapRealm, $localAuthorizingRealm
在 LocalAuthorizingRealm 中,用户登录进行认证之前,先将该用户的其他session移除:
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String userName = (String)authenticationToken.getPrincipal();
//处理session
DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
DefaultWebSessionManager sessionManager = (DefaultWebSessionManager)securityManager.getSessionManager();
Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();//获取当前已登录的用户session列表
for(Session session:sessions){
//清除该用户以前登录时保存的session
if(userName.equals(String.valueOf(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)))) {
sessionManager.getSessionDAO().delete(session);
}
}
String pwd = null;
return new SimpleAuthenticationInfo(userName,pwd,getName());
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
