forked from Vino007/javaEEScaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspring-shiro-web.xml
137 lines (118 loc) · 7.9 KB
/
spring-shiro-web.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
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
<!-- 凭证匹配器,解密使用,注入到realm中使用 -->
<!-- 使用自定义的credentialMatcher,实现了多次参试失败后锁定用户的功能 -->
<bean id="credentialsMatcher" class="com.vino.scaffold.shiro.credential.RetryLimitHashedCredentialsMatcher">
<constructor-arg ref="cacheManager"/>
<!-- 加密解密的算法要一样,迭代数也是 -->
<property name="hashAlgorithmName" value="md5"/>
<property name="hashIterations" value="2"/>
<property name="storedCredentialsHexEncoded" value="true"/>
</bean>
<!-- Realm实现 -->
<bean id="userRealm" class="com.vino.scaffold.shiro.realm.UserRealm">
<property name="userService" ref="userService"/>
<property name="credentialsMatcher" ref="credentialsMatcher"/>
<property name="cachingEnabled" value="true"/>
<property name="authenticationCachingEnabled" value="true"/>
<property name="authenticationCacheName" value="authenticationCache"/>
<property name="authorizationCachingEnabled" value="true"/>
<property name="authorizationCacheName" value="authorizationCache"/>
</bean>
<!-- 会话ID生成器 -->
<bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>
<!-- 会话Cookie模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="${shiro.uid.cookie.name}"/>
<property name="httpOnly" value="true"/>
<property name="maxAge" value="${shiro.uid.cookie.maxAge}"/><!-- 单位秒 -->
</bean>
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="${shiro.uid.rememeberMe.cookie.name}"/>
<property name="httpOnly" value="true"/>
<property name="maxAge" value="${shiro.uid.rememeberMe.cookie.maxAge}"/><!-- 30天 -->
</bean>
<!-- 会话DAO -->
<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
<property name="sessionIdGenerator" ref="sessionIdGenerator"/>
</bean>
<!-- 会话验证调度器 -->
<bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
<property name="sessionValidationInterval" value="${shiro.session.validation.interval}"/><!-- 30分钟验证一次会话是否过期 -->
<property name="sessionManager" ref="sessionManager"/>
</bean>
<!-- 会话管理器 -->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="globalSessionTimeout" value="${shiro.session.globalSessionTimeout}"/><!-- 会话超时时间 两小时 单位毫秒 -->
<property name="deleteInvalidSessions" value="true"/>
<property name="sessionValidationSchedulerEnabled" value="true"/>
<property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
<property name="sessionDAO" ref="sessionDAO"/>
<property name="sessionIdCookieEnabled" value="true"/>
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>
<!-- rememberMe管理器 -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<!-- rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位)-->
<property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('${shiro.uid.rememeberMe.cookie.base64.cipherKey}')}"/>
<property name="cookie" ref="rememberMeCookie"/>
</bean>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm"/>
<property name="sessionManager" ref="sessionManager"/>
<property name="cacheManager" ref="cacheManager"/>
<property name="rememberMeManager" ref="rememberMeManager"/>
</bean>
<!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
<!-- 基于Form表单的身份验证过滤器 -->
<bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
<property name="usernameParam" value="username"/>
<property name="passwordParam" value="password"/>
<property name="loginUrl" value="${shiro.login.url}"/>
<property name="rememberMeParam" value="rememberMe"/><!--即rememberMe请求参数名,请求参数是boolean类型,true表示rememberMe。 -->
</bean>
<bean id="monitorFilter" class="com.vino.scaffold.shiro.filter.MonitorAccessControlFilter"></bean>
<!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 此处使用ShiroFilterFactoryBean来创建ShiroFilter过滤器;filters属性用于定义自己的过滤器,
即ini配置中的[filters]部分;filterChainDefinitions用于声明url和filter的关系,即ini配置中的[urls]部分。 -->
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="${shiro.login.url}"/><!-- 即访问这些地址时会首先判断用户有没有登录,如果没有登录默会跳转到登录页面,设置loginUrl属性,默认是/login.jsp -->
<property name="unauthorizedUrl" value="${shiro.unauthorizedUrl}"/><!-- 没有权限的时候跳转页面 -->
<property name="successUrl" value="/"></property>
<property name="filters">
<util:map>
<entry key="authc" value-ref="formAuthenticationFilter"/><!-- filters该属性用于定义自定义的过滤器 -->
<entry key="monitor" value-ref="monitorFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions"><!-- 用于申明过滤器与url的关系,过滤器采用短路原则,越上面越先匹配 -->
<value>
/login = authc <!--authc拦截器会判断用户是否是通过Subject.login(isAuthenticated()==true)登录的,如果是才放行,否则会跳转到登录页面叫你重新登录 -->
/logout = logout <!-- 注销拦截器 -->
/druid = monitor <!-- 监控拦截器 -->
/register=anon <!-- 允许匿名访问 -->
/prepareRegister=anon
/resources/**=anon <!-- 不拦截静态资源!! -->
/** = user <!-- isRemenber=true或者是通过login登录的就可以通过 -->
</value>
</property>
</bean>
<!-- Shiro生命周期处理器-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
</beans>