我這里 shiro 并沒有集成 springMVC,直接使用 ini 配置文件。
shiro.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[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移除:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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()); } |
當session刪除之后,必須有客戶端與服務器端的交互,shiro才能進行認證判斷。在與服務器交互時,subject信息截圖如下:
此時的登錄的用戶認證已經失效,可以對客戶端做出響應。
以上所述是小編給大家介紹的shiro實現單點登錄(一個用戶同一時刻只能在一個地方登錄),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!