10
10
import org .apache .shiro .spring .security .interceptor .AuthorizationAttributeSourceAdvisor ;
11
11
import org .apache .shiro .spring .web .ShiroFilterFactoryBean ;
12
12
import org .apache .shiro .web .mgt .DefaultWebSecurityManager ;
13
+ import org .apache .shiro .web .session .mgt .DefaultWebSessionManager ;
14
+ import org .crazycake .shiro .RedisCacheManager ;
15
+ import org .crazycake .shiro .RedisManager ;
16
+ import org .crazycake .shiro .RedisSessionDAO ;
13
17
import org .springframework .beans .factory .annotation .Autowired ;
18
+ import org .springframework .beans .factory .annotation .Value ;
14
19
import org .springframework .context .annotation .Bean ;
15
20
import org .springframework .context .annotation .Configuration ;
16
21
@@ -26,6 +31,15 @@ public class ShiroConfig {
26
31
@ Autowired (required = false )
27
32
private ResourcesService resourcesService ;
28
33
34
+ @ Value ("${spring.redis.host}" )
35
+ private String host ;
36
+
37
+ @ Value ("${spring.redis.port}" )
38
+ private int port ;
39
+
40
+ @ Value ("${spring.redis.timeout}" )
41
+ private int timeout ;
42
+
29
43
/**
30
44
* ShiroDialect,为了在thymeleaf里使用shiro的标签的bean
31
45
* @return
@@ -91,6 +105,10 @@ public SecurityManager securityManager(){
91
105
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager ();
92
106
//设置realm.
93
107
securityManager .setRealm (myShiroRealm ());
108
+ // 自定义缓存实现 使用redis
109
+ securityManager .setCacheManager (cacheManager ());
110
+ // 自定义session管理 使用redis
111
+ securityManager .setSessionManager (sessionManager ());
94
112
return securityManager ;
95
113
}
96
114
@@ -132,6 +150,21 @@ public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(S
132
150
return authorizationAttributeSourceAdvisor ;
133
151
}
134
152
153
+ /**
154
+ * 配置shiro redisManager
155
+ * 使用的是shiro-redis开源插件
156
+ * @return
157
+ */
158
+ public RedisManager redisManager () {
159
+ RedisManager redisManager = new RedisManager ();
160
+ redisManager .setHost (host );
161
+ redisManager .setPort (port );
162
+ redisManager .setExpire (1800 );// 配置缓存过期时间
163
+ redisManager .setTimeout (timeout );
164
+ // redisManager.setPassword(password);
165
+ return redisManager ;
166
+ }
167
+
135
168
/**
136
169
* cacheManager 缓存 redis实现
137
170
* 使用的是shiro-redis开源插件
@@ -143,4 +176,25 @@ public RedisCacheManager cacheManager() {
143
176
return redisCacheManager ;
144
177
}
145
178
179
+ /**
180
+ * RedisSessionDAO shiro sessionDao层的实现 通过redis
181
+ * 使用的是shiro-redis开源插件
182
+ */
183
+ @ Bean
184
+ public RedisSessionDAO redisSessionDAO () {
185
+ RedisSessionDAO redisSessionDAO = new RedisSessionDAO ();
186
+ redisSessionDAO .setRedisManager (redisManager ());
187
+ return redisSessionDAO ;
188
+ }
189
+
190
+ /**
191
+ * Session Manager
192
+ * 使用的是shiro-redis开源插件
193
+ */
194
+ @ Bean
195
+ public DefaultWebSessionManager sessionManager () {
196
+ DefaultWebSessionManager sessionManager = new DefaultWebSessionManager ();
197
+ sessionManager .setSessionDAO (redisSessionDAO ());
198
+ return sessionManager ;
199
+ }
146
200
}
0 commit comments