8
8
import org .apache .shiro .authc .*;
9
9
import org .apache .shiro .authz .AuthorizationInfo ;
10
10
import org .apache .shiro .authz .SimpleAuthorizationInfo ;
11
+ import org .apache .shiro .mgt .RealmSecurityManager ;
11
12
import org .apache .shiro .realm .AuthorizingRealm ;
12
13
import org .apache .shiro .session .Session ;
13
14
import org .apache .shiro .subject .PrincipalCollection ;
15
+ import org .apache .shiro .subject .SimplePrincipalCollection ;
16
+ import org .apache .shiro .subject .support .DefaultSubjectContext ;
14
17
import org .apache .shiro .util .ByteSource ;
18
+ import org .crazycake .shiro .RedisSessionDAO ;
19
+ import org .springframework .beans .factory .annotation .Autowired ;
15
20
16
21
import javax .annotation .Resource ;
17
- import java .util .HashMap ;
18
- import java .util .List ;
19
- import java .util .Map ;
22
+ import java .util .*;
20
23
21
24
/**
22
25
* Created by yangqj on 2017年4月21日.
@@ -29,6 +32,9 @@ public class MyShiroRealm extends AuthorizingRealm {
29
32
@ Resource
30
33
private ResourcesService resourcesService ;
31
34
35
+ @ Autowired
36
+ private RedisSessionDAO redisSessionDAO ;
37
+
32
38
//授权
33
39
@ Override
34
40
protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principalCollection ) {
@@ -67,14 +73,40 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
67
73
return authenticationInfo ;
68
74
}
69
75
76
+
70
77
/**
71
- * 指定principalCollection 清除
78
+ * 根据userId 清除当前session存在的用户的权限缓存
79
+ * @param userIds 已经修改了权限的userId
72
80
*/
73
- /* public void clearCachedAuthorizationInfo(PrincipalCollection principalCollection) {
74
-
75
- SimplePrincipalCollection principals = new SimplePrincipalCollection(
76
- principalCollection, getName());
77
- super.clearCachedAuthorizationInfo(principals);
81
+ public void clearUserAuthByUserId (List <Integer > userIds ){
82
+ if (null == userIds || userIds .size () == 0 ) return ;
83
+ //获取所有session
84
+ Collection <Session > sessions = redisSessionDAO .getActiveSessions ();
85
+ //定义返回
86
+ List <SimplePrincipalCollection > list = new ArrayList <SimplePrincipalCollection >();
87
+ for (Session session :sessions ){
88
+ //获取session登录信息。
89
+ Object obj = session .getAttribute (DefaultSubjectContext .PRINCIPALS_SESSION_KEY );
90
+ if (null != obj && obj instanceof SimplePrincipalCollection ){
91
+ //强转
92
+ SimplePrincipalCollection spc = (SimplePrincipalCollection )obj ;
93
+ //判断用户,匹配用户ID。
94
+ obj = spc .getPrimaryPrincipal ();
95
+ if (null != obj && obj instanceof User ){
96
+ User user = (User ) obj ;
97
+ System .out .println ("user:" +user );
98
+ //比较用户ID,符合即加入集合
99
+ if (null != user && userIds .contains (user .getId ())){
100
+ list .add (spc );
101
+ }
102
+ }
103
+ }
104
+ }
105
+ RealmSecurityManager securityManager =
106
+ (RealmSecurityManager ) SecurityUtils .getSecurityManager ();
107
+ MyShiroRealm realm = (MyShiroRealm )securityManager .getRealms ().iterator ().next ();
108
+ for (SimplePrincipalCollection simplePrincipalCollection : list ) {
109
+ realm .clearCachedAuthorizationInfo (simplePrincipalCollection );
110
+ }
78
111
}
79
- */
80
112
}
0 commit comments