我這里 shiro 并沒(méi)有集成 springMVC,直接使用 ini 配置文件。
shiro.ini
[main]# Objects and their properties are defined here,# Such as the securityManager, Realms and anything# else needed to build the SecurityManagerauthc.loginUrl = /login.jspauthc.successUrl = /web/index.jsp#cache managerbuiltInCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManagersecurityManager=org.apache.shiro.web.mgt.DefaultWebSecurityManagersecurityManager.cacheManager = $builtInCacheManagersecurityManager.sessionManager=$sessionManager#session 必須配置session,強(qiáng)制退出時(shí),通過(guò)將session移除實(shí)現(xiàn)sessionManager=org.apache.shiro.web.session.mgt.DefaultWebSessionManagersessionManager.sessionDAO=$sessionDAOsessionDAO=org.apache.shiro.session.mgt.eis.MemorySessionDAO# Create ldap realmldapRealm = org.apache.shiro.realm.ldap.JndiLdapRealm#......# Configure JDBC realm datasourcedataSource = org.postgresql.ds.PGPoolingDataSource#.......# Create JDBC realm.jdbcRealm.permissionsLookupEnabled = truejdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealmjdbcRealm.userRolesQuery = ......jdbcRealm.permissionsQuery = ......jdbcRealm.dataSource = $dataSource#self realmlocalAuthorizingRealm = com.redbudtek.shiro.LocalAuthorizingRealmsecurityManager.realms = $ldapRealm, $localAuthorizingRealm
在 LocalAuthorizingRealm 中,用戶(hù)登錄進(jìn)行認(rèn)證之前,先將該用戶(hù)的其他session移除:
@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {String userName = (String)authenticationToken.getPrincipal();//處理sessionDefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();DefaultWebSessionManager sessionManager = (DefaultWebSessionManager)securityManager.getSessionManager();Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();//獲取當(dāng)前已登錄的用戶(hù)session列表for(Session session:sessions){//清除該用戶(hù)以前登錄時(shí)保存的sessionif(userName.equals(String.valueOf(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)))) {sessionManager.getSessionDAO().delete(session);}}String pwd = null;return new SimpleAuthenticationInfo(userName,pwd,getName());}
當(dāng)session刪除之后,必須有客戶(hù)端與服務(wù)器端的交互,shiro才能進(jìn)行認(rèn)證判斷。在與服務(wù)器交互時(shí),subject信息截圖如下:

此時(shí)的登錄的用戶(hù)認(rèn)證已經(jīng)失效,可以對(duì)客戶(hù)端做出響應(yīng)。
以上所述是小編給大家介紹的shiro實(shí)現(xiàn)單點(diǎn)登錄(一個(gè)用戶(hù)同一時(shí)刻只能在一個(gè)地方登錄),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注