集群部署的落地实现之springshiroredis架构第五篇shiro.xml文件配置spring集成shiro配置
定义spring容器管理的全局cacheManager
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
c:redisOperations-ref="redisTemplate">
<property name="defaultExpiration" value="600"/>
<property name="usePrefix" value="true"/>
<!-- cacheName 缓存超时配置,半小时,一小时,一天 -->
<property name="expires">
<map key-type="java.lang.String" value-type="java.lang.Long">
<entry key="halfHour" value="1800"/>
<entry key="hour" value="3600"/>
<entry key="oneDay" value="86400"/>
<entry key="oneWeek" value="604800"/>
<entry key="oneMonth" value="2592000"/>
<!-- shiro cache keys -->
<entry key="authorizationCache" value="${redis.cache.author}"/>
<entry key="authenticationCache" value="${redis.cache.authen}"/>
<entry key="activeSessionCache" value="${redis.cache.active.session}"/>
</map>
</property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
default-lazy-init="true">
<description>Shiro安全配置</description>
<bean class="XXX.captcha.DreamCaptcha">
<property name="cacheManager" ref="shiroJedisCacheManager"/>
<!-- 复用半小时缓存 -->
<property name="cacheName" value="fortyMinutes"/>
</bean>
<!--安全管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--设置自定义Realm-->
<property name="realm" ref="shiroDbRealm"/>
<!--将缓存管理器,交给安全管理器-->
<property name="cacheManager" ref="shiroJedisCacheManager"/>
<!-- 记住密码管理 -->
<property name="rememberMeManager" ref="rememberMeManager"/>
<property name="sessionManager" ref="sessionManager"/>
</bean>
<!-- 項目自定义的Realm -->
<bean id="shiroDbRealm" class="XXX.ShiroDbRealm">
<constructor-arg index="0" name="cacheManager" ref="shiroJedisCacheManager"/>
<constructor-arg index="1" name="matcher" ref="credentialsMatcher"/>
<!-- 启用身份验证缓存,即缓存AuthenticationInfo信息,默认false -->
<property name="authenticationCachingEnabled" value="false"/>
<!-- 缓存AuthenticationInfo信息的缓存名称 -->
<property name="authenticationCacheName" value="authenticationCache"/>
<!-- 缓存AuthorizationInfo信息的缓存名称 -->
<property name="authorizationCacheName" value="authorizationCache"/>
</bean>
<!-- 记住密码Cookie -->
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="rememberMe"/>
<property name="httpOnly" value="true"/>
<!-- 7天,采用spring el计算方便修改[细节决定成败]! -->
<property name="maxAge" value="#{7 * 24 * 60 * 60}"/>
</bean>
<!-- rememberMe管理器,cipherKey生成见{@code Base64Test.java} -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode("5aaC5qKm5oqA5pyvAAAAAA==")}"/>
<property name="cookie" ref="rememberMeCookie"/>
</bean>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager"/>
<!-- 默认的登陆访问url -->
<property name="loginUrl" value="/XXX"/>
<!-- 登陆成功后跳转的url -->
<property name="successUrl" value="/XXX"/>
<!-- 没有权限跳转的url -->
<property name="unauthorizedUrl" value="/XXX"/>
<property name="filterChainDefinitions">
<value>
<!--
anon 不需要认证
authc 需要认证
user 验证通过或RememberMe登录的都可以
-->
/** = user
</value>
</property>
<property name="filters">
<map>
<entry key="user" value-ref="ajaxSessionFilter" />
</map>
</property>
</bean>
<!-- ajax session超时时处理 -->
<bean id="ajaxSessionFilter" class="XXX.ShiroAjaxSessionFilter"/>
<!-- 利用spring-cache -->
<bean id="shiroSpringCacheManager" class="com.gta.yyyf.commons.shiro.cache.ShiroSpringCacheManager">
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!-- 会话管理器org.apache.shiro.web.session.mgt.DefaultWebSessionManager -->
<bean id="sessionManager" class="XXX.ShiroSessionManager">
<!-- 设置全局会话超时时间 四十分钟 -->
<property name="globalSessionTimeout" value="#{40 * 60 * 1000}"/>
<!-- url上带sessionId 默认为true -->
<!-- <property name="sessionIdUrlRewritingEnabled" value="false"/> -->
<property name="sessionDAO" ref="sessionDAO"/>
<property name ="sessionListeners">
<list>
<ref bean="userSessionListener"/>
</list>
</bean>
<!-- 会话DAO 用于会话的CRUD -->
<bean id="sessionDAO" class="XXX.RedisSessionDAO">
</bean>
<!-- 在方法中 注入 securityManager ,进行代理控制 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
<!-- shiro密码加密配置 -->
<bean id="passwordHash" class="XXX.PasswordHash">
<!-- 密码加密 1次md5,增强密码可修改此处 -->
<property name="algorithmName" value="md5"/>
<property name="hashIterations" value="1"/>
</bean>
<!-- 密码错误5次锁定半小时 -->
<bean id="credentialsMatcher" class="XXX.RetryLimitCredentialsMatcher">
<constructor-arg ref="shiroJedisCacheManager"/>
<property name="retryLimitCacheName" value="halfHour"/>
<property name="passwordHash" ref="passwordHash"/>
</bean>
</beans>
以上是 集群部署的落地实现之springshiroredis架构第五篇shiro.xml文件配置spring集成shiro配置 的全部内容, 来源链接: utcz.com/z/518875.html