分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

Apache-Shiro分布式环境配置(与redis集成)(转)

发布时间:2023-09-06 01:41责任编辑:郭大石关键词:配置redis

原文戳我

前段时间项目要用到权限控制的相关模块,经过讨论决定采用Apache下面的Shiro开源框架进行身份校验与权限控制,因项目需部署在集群环境下,所以需要分布式的支持,故配置了Redis作为权限数据的存储,这里简单的记录下相关的配置

applicationContext-shiro.xml

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<beanid="lifecycleBeanPostProcessor"class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<beanid="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<propertyname="authenticator"ref="authenticator"/>
<propertyname="sessionManager"ref="sessionManager"/>
<!--<propertyname="sessionMode"value="http"/>-->
<propertyname="cacheManager"ref="redisCacheManager"/>
<propertyname="realms">
<list>
<reflocal="customRealm"/>
</list>
</property>
</bean>
<beanclass="com.bbk.security.realm.CustomRealm"id="customRealm">
<propertyname="credentialsMatcher"ref="hashedCredentialsMatcher"/>
</bean>
<beanid="redisCacheManager"class="com.bbk.security.cache.RedisCacheManager"><!--自定义cacheManager-->
<propertyname="redisManager"ref="redisManager"/>
</bean>
<beanid="redisCache"class="com.bbk.security.cache.RedisCache"><!--自定义cacheManager-->
<constructor-argref="redisManager"></constructor-arg>
</bean>
<beanid="roleOR"class="com.bbk.filter.CustomAuthorizationFilter"/>
<beanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--调用我们配置的权限管理器-->
<propertyname="securityManager"ref="securityManager"/>
</bean>
<beanid="authenticator"class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"/>
<beanid="sessionManager"class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<propertyname="sessionDAO"ref="redisShiroSessionDAO"/>
<propertyname="globalSessionTimeout"value="${shiro.session.timeout}"/><!--会话过期时间,在配置文件里面配置-->
<propertyname="sessionValidationInterval"value="3000000"/>
<propertyname="sessionValidationSchedulerEnabled"value="true"/>
</bean>
<beanid="redisShiroSessionDAO"class="com.bbk.security.cache.RedisSessionDAO">
<propertyname="redisManager"ref="redisManager"/>
</bean>
<beanid="redisManager"class="com.bbk.security.cache.RedisManager"></bean>
<beanid="hashedCredentialsMatcher"class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"><!--密钥的算法-->
<propertyname="hashAlgorithmName"value="MD5"/>
<propertyname="storedCredentialsHexEncoded"value="true"/>
<propertyname="hashIterations"value="1"/>
</bean>
<!--copyto‘spring-mvc.xml‘-->
<!--<beandepends-on="lifecycleBeanPostProcessor"/>
<bean>
<propertyname="securityManager"ref="securityManager"/></bean>-->
</beans>

RedisCache

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved