目標:讓Shiro整合ehcache,提供緩存realm數據的功能。
1.引入encache配置文件,配置緩存

1 <!-- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 2 --><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> 3 4 <!-- 磁盤上的緩存的臨時目錄 ,默認是系統的臨時目錄,也可以手動指定一個目錄--> 5 <diskStore path="java.io.tmpdir"/> 6 <!-- 默認的緩存區域的默認策略 --> 7 <!-- 8 maxElementsInMemory:內存中元素最大存放的個數 9 eternal:緩存的對象是否永生不死。一般都是false。 10 timeToIdleSeconds:發呆的時間,多長時間不用,就干掉,秒 11 timeToLiveSeconds:存活的時間,活夠了就干掉,秒 12 maxElementsOnDisk:硬盤上最大存放的元素的個數,如果內存10000個滿了,就往硬盤上存。 13 memoryStoreEvictionPolicy:內存中存儲和銷毀元素的策略:默認使用LRU,解決的是緩存元素滿了怎么辦。 14 策略有三個:LRU、LFU、FIFO 15 LRU:最少使用被清理,次數 16 LFU:時間,閑置最長的時間 17 FIFO:管道策略,先進先出 18 --> 19 <defaultCache 20 maxElementsInMemory="10000" 21 eternal="false" 22 timeToIdleSeconds="120" 23 timeToLiveSeconds="120" 24 maxElementsOnDisk="10000000" 25 diskExpiryThreadIntervalSeconds="120" 26 memoryStoreEvictionPolicy="LRU"> 27 <persistence strategy="localTempSwap"/> 28 </defaultCache> 29 <!-- Spring整合的菜單緩存 --> 30 <cache name="bos_menu_cache" 31 maxElementsInMemory="10000" 32 eternal="false" 33 timeToIdleSeconds="120" 34 timeToLiveSeconds="120" 35 maxElementsOnDisk="10000000" 36 diskExpiryThreadIntervalSeconds="120" 37 memoryStoreEvictionPolicy="LRU"> 38 <persistence strategy="localTempSwap"/> 39 </cache> 40 <!-- Shiro權限緩存-認證 --> 41 <cache name="bos_realm_authentication_cache" 42 maxElementsInMemory="10000" 43 eternal="false" 44 timeToIdleSeconds="120" 45 timeToLiveSeconds="120" 46 maxElementsOnDisk="10000000" 47 diskExpiryThreadIntervalSeconds="120" 48 memoryStoreEvictionPolicy="LRU"> 49 <persistence strategy="localTempSwap"/> 50 </cache> 51 <!-- Shiro權限緩存-授權 --> 52 <cache name="bos_realm_authorization_cache" 53 maxElementsInMemory="10000" 54 eternal="false" 55 timeToIdleSeconds="120" 56 timeToLiveSeconds="120" 57 maxElementsOnDisk="10000000" 58 diskExpiryThreadIntervalSeconds="120" 59 memoryStoreEvictionPolicy="LRU"> 60 <persistence strategy="localTempSwap"/> 61 </cache> 62 </ehcache>
2.引入坐標(其他坐標這里略)

1 <!-- shiro整合ehcache --> 2 <dependency> 3 <groupId>org.apache.shiro</groupId> 4 <artifactId>shiro-ehcache</artifactId> 5 <version>1.3.2</version> 6 </dependency>
3.在Spring中配置shiro緩存

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <!-- 配置Shiro核心Filter,bean的id必須和過濾器的名字一樣 --> 8 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 9 <!-- 安全管理器 --> 10 <property name="securityManager" ref="securityManager" /> 11 <!-- 未認證,跳轉到哪個頁面 ,如果認證失敗,跳轉的默認頁面 --> 12 <property name="loginUrl" value="/login.html" /> 13 <!-- 登錄頁面頁面,如果認證成功,則默認跳轉的頁面 --> 14 <property name="successUrl" value="/index.html" /> 15 <!-- 如果沒有授權,則默認跳轉到該頁面 --> 16 <property name="unauthorizedUrl" value="/unauthorized.html" /> 17 <!-- shiro URL控制過濾器規則:配置的小過濾器鏈(過濾器棧):執行從上倒下有順序 --> 18 <property name="filterChainDefinitions"> 19 <value> 20 /login.html* = anon 21 /user_login.action* = anon 22 /validatecode.jsp* = anon 23 /css/** = anon 24 /js/** = anon 25 /images/** = anon 26 /services/** = anon 27 /pages/base/courier.html* = perms[courier:list] 28 /pages/base/area.html* = roles[base] 29 /** = authc 30 </value> 31 </property> 32 </bean> 33 34 <!-- 安全管理器 --> 35 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 36 <property name="realm" ref="bosRealm"></property> 37 <!-- 開啟Shiro緩存功能,需要在shiro安全管理器中注入shiro的 平台緩存管理器 --> 38 <property name="cacheManager" ref="shiroCacheManager" /> 39 </bean> 40 41 <!-- 配置Shiro的bean后處理器:用來初始化Shiro的bean在spring中--> 42 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 43 <!-- 開啟Shiro注解 --> 44 <!-- Enable Shiro Annotations for Spring-configured beans. 45 Only run after --> 46 <!-- the lifecycleBeanProcessor has run: 47 depends-on:當前bean初始化時,必須依賴於指定的bean,(指定的 48 bean必須先初始化) 49 下面的兩個bean配置:傳統的aop編程:增強、切點、切面 50 --> 51 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" 52 depends-on="lifecycleBeanPostProcessor"/> 53 54 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 55 <!-- 必須注入安全管理器 --> 56 <property name="securityManager" ref="securityManager" /> 57 </bean> 58 59 <!-- shiro整合echcache的緩存配置 --> 60 <!-- 配置Shiro的平台緩存管理 --> 61 <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> 62 <!-- 注入ehcache的對象 --> 63 <property name="cacheManager" ref="ehCacheManager" /> 64 </bean> 65 66 </beans>
4.在reaml對象中指定緩存權限的數據的區域

1 @Component("bosRealm") 2 public class BosRealm extends AuthorizingRealm{ 3 4 //只需要向父類注入緩存區域即可 5 //認證緩存區域 6 @Value("bos_realm_authentication_cache") 7 //方法上注入按照參數注入,和方法名無關 8 public void setSuperAuthenticationCacheName(String authenticationCacheName){ 9 super.setAuthenticationCacheName(authenticationCacheName); 10 } 11 //授權緩存區域 12 @Value("bos_realm_authorization_cache") 13 public void setSuperAuthorizationCacheName(String authorizationCacheName){ 14 super.setAuthorizationCacheName(authorizationCacheName); 15 }