Shiro教程(六)Shiro整體的配置文件
Shiro + SSM(框架) + Freemarker(jsp)講解的權(quán)限控制Demo,還不趕快去下載?
因為 Shrio 和整體項目結(jié)合的話,細(xì)節(jié)問題還是比較多。我要側(cè)重講一些地方。下面我先把我整個項目的 Shiro 配置文件先貼出來,如果你需要哪個類的實現(xiàn)方式,你可以針對性的查找,或者問我。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<description>== Shiro Components ==</description>
<!-- 會話ID生成器 -->
<bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>
<!-- 會話Cookie模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<!--cookie的name,我故意取名叫xxxxbaidu -->
<constructor-arg value="v_v-s-baidu"/>
<property name="httpOnly" value="true"/>
<!--cookie的有效時間 -->
<property name="maxAge" value="-1"/>
<!-- 配置存儲Session Cookie的domain為 一級域名 -->
<property name="domain" value=".itboy.net"/>
</bean>
<!-- custom shiro session listener -->
<bean id="customSessionListener" class="com.sojson.core.shiro.listenter.CustomSessionListener">
<property name="shiroSessionRepository" ref="jedisShiroSessionRepository"/>
</bean>
<!-- 用戶信息記住我功能的相關(guān)配置 -->
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="v_v-re-baidu"/>
<property name="httpOnly" value="true"/>
<!-- 配置存儲rememberMe Cookie的domain為 一級域名 -->
<property name="domain" value=".itboy.net"/>
<property name="maxAge" value="2592000"/><!-- 30天時間,記住我30天 -->
</bean>
<!-- rememberMe管理器 -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<!-- rememberMe cookie加密的密鑰 建議每個項目都不一樣 默認(rèn)AES算法 密鑰長度(128 256 512 位)-->
<property name="cipherKey"
value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>
<property name="cookie" ref="rememberMeCookie"/>
</bean>
<!-- custom shiro session listener -->
<bean id="customShiroSessionDAO" class="com.sojson.core.shiro.CustomShiroSessionDAO">
<property name="shiroSessionRepository" ref="jedisShiroSessionRepository"/>
<property name="sessionIdGenerator" ref="sessionIdGenerator"/>
</bean>
<!-- 會話驗證調(diào)度器 -->
<bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
<property name="interval" value="${session.validate.timespan}"/>
<property name="sessionManager" ref="sessionManager"/>
</bean>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="sampleRealm"/>
<property name="sessionManager" ref="sessionManager"/>
<property name="rememberMeManager" ref="rememberMeManager"/>
<property name="cacheManager" ref="customShiroCacheManager"/>
</bean>
<!-- 用戶緩存 -->
<bean id="customShiroCacheManager" class="com.sojson.core.shiro.cache.impl.CustomShiroCacheManager">
<property name="shiroCacheManager" ref="jedisShiroCacheManager"/>
</bean>
<!-- shiro 緩存實現(xiàn),對ShiroCacheManager,我是采用redis的實現(xiàn) -->
<bean id="jedisShiroCacheManager" class="com.sojson.core.shiro.cache.impl.JedisShiroCacheManager">
<property name="jedisManager" ref="jedisManager"/>
</bean>
<!-- redis 的緩存 -->
<bean id="jedisManager" class="com.sojson.core.shiro.cache.JedisManager">
<property name="jedisPool" ref="jedisPool"/>
</bean>
<!-- 相當(dāng)于調(diào)用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>
<!-- 授權(quán) 認(rèn)證 -->
<bean id="sampleRealm" class="com.sojson.core.shiro.token.SampleRealm" ></bean>
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionValidationInterval" value="1800000"/> <!-- 相隔多久檢查一次session的有效性 -->
<property name="globalSessionTimeout" value="1800000"/> <!-- session 有效時間為半小時 (毫秒單位)-->
<property name="sessionDAO" ref="customShiroSessionDAO"/>
<property name="sessionListeners">
<list>
<ref bean="customSessionListener"/>
</list>
</property>
<property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
<property name="sessionValidationSchedulerEnabled" value="true"/>
<property name="deleteInvalidSessions" value="true"/>
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>
<bean id="jedisShiroSessionRepository" class="com.sojson.core.shiro.cache.JedisShiroSessionRepository" >
<property name="jedisManager" ref="jedisManager"/>
</bean>
<!--
自定義角色過濾器 支持多個角色可以訪問同一個資源 eg:/home.jsp = authc,roleOR[admin,user]
用戶有admin或者user角色 就可以訪問
-->
<!-- 認(rèn)證數(shù)據(jù)庫存儲-->
<bean id="shiroManager" class="com.sojson.core.shiro.service.impl.ShiroManagerImpl"/>
<bean id="login" class="com.sojson.core.shiro.filter.LoginFilter"/>
<bean id="role" class="com.sojson.core.shiro.filter.RoleFilter"/>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/u/login.shtml" />
<!-- TODO 待提取 -->
<property name="successUrl" value="/" />
<property name="unauthorizedUrl" value="/?login" />
<!-- 初始配置,現(xiàn)采用自定義 -->
<!-- <property name="filterChainDefinitions" >-->
<!-- <value>-->
<!-- /** = anon-->
<!-- /page/login.jsp = anon-->
<!-- /page/register/* = anon-->
<!-- /page/index.jsp = authc-->
<!-- /page/addItem* = authc,roles[數(shù)據(jù)管理員]-->
<!-- /page/file* = authc,roleOR[普通用戶,數(shù)據(jù)管理員]-->
<!-- /page/listItems* = authc,roleOR[數(shù)據(jù)管理員,普通用戶]-->
<!-- /page/showItem* = authc,roleOR[數(shù)據(jù)管理員,普通用戶]-->
<!-- /page/updateItem*=authc,roles[數(shù)據(jù)管理員]-->
<!-- </value>-->
<!-- </property>-->
<!-- 讀取初始自定義權(quán)限內(nèi)容-->
<property name="filterChainDefinitions" value="#{shiroManager.loadFilterChainDefinitions()}"/>
<property name="filters">
<util:map>
<entry key="login" value-ref="login"></entry>
<entry key="role" value-ref="role"></entry>
</util:map>
</property>
</bean>
<!-- Shiro生命周期處理器-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- ============================================================================ -->
</beans>
后面會每個類的實現(xiàn)方式提供出來。
版權(quán)所屬:SO JSON在線解析
原文地址:http://suancuo.cn/blog/136.html
轉(zhuǎn)載時必須以鏈接形式注明原始出處及本聲明。
如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。

