shiro 权限集成Ehcache 配置 学习记录(二)

1、加入依赖

 1 <dependency>
 2                 <groupId>org.apache.shiro</groupId>
 3                 <artifactId>shiro-ehcache</artifactId>
 4                 <version>${shiro.version}</version>
 5                 <exclusions>
 6                     <exclusion>
 7                         <artifactId>slf4j-api</artifactId>
 8                         <groupId>org.slf4j</groupId>
 9                     </exclusion>
10                 </exclusions>
11             </dependency>

2、在项目的resource目录下新建立:ehcache-shiro.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ehcache updateCheck="false" name="shiroCache">
 3 
 4     <diskStore path="C:shiroehcache" />
 5 <!--     <diskStore path="java.io.tmpdir"/> -->
 6 
 7     <!--   
 8     eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。  
 9     maxElementsInMemory:缓存中允许创建的最大对象数  
10     overflowToDisk:内存不足时,是否启用磁盘缓存。  
11     timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,  两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。  
12     timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。  
13     memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。  
14     diskPersistent:设定在虚拟机重启时是否进行磁盘存储,默认为false
15     diskExpiryThreadIntervalSeconds: 属性可以设置该线程执行的间隔时间(默认是120秒,不能太小
16     1 FIFO,先进先出  
17     2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。  
18     3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。  
19     -->  
20     <defaultCache
21             maxElementsInMemory="10000"
22             eternal="false"
23             timeToIdleSeconds="120"
24             timeToLiveSeconds="120"
25             overflowToDisk="false"
26             diskPersistent="false"
27             diskExpiryThreadIntervalSeconds="120"
28             />
29 
30     <cache name="activeSessionCache"
31            maxElementsInMemory="10000"
32            eternal="true"
33            overflowToDisk="false"
34            diskPersistent="true"
35            diskExpiryThreadIntervalSeconds="600"/>
36            
37     <cache name="shiro.authorizationCache"
38            maxElementsInMemory="100"
39            eternal="false"
40            timeToLiveSeconds="600"
41            overflowToDisk="false"/>
42            
43 </ehcache>

3、在shiro的配置文件中  securityManager  的bean中 增加  cacheManager 属性配置:如 下 文件中 标红的 属性

1 <!-- 1、安全管理器 -->
2     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
3         <property name="realm" ref="shiroDbRealm"></property>
4         <!-- 设置缓存管理器为 ehcache -->
5         <property name="cacheManager" ref="shiroEhcacheManager"></property>
6     </bean>

4、增加Ehcache的配置:如

1 <!-- 用户授权信息Cache, 采用EhCache -->
2     <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
3         <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"></property>
4     </bean>

 第二:怎么清空系统的缓存

1、编写自定义的realm:如:  ShiroDBRealm

2、注解  ShiroDBRealm  为一个  @Component  组件

3、编写方法清理缓存:

 1 /**
 2      * 
 3      * @Description: 权限修改生效后,立即刷新清空缓存,则可以实现用户不退出生效新的权限
 4      * 
 5      * @author admin
 6      * @date 2016年9月29日 下午9:34:07
 7      */
 8     public void clearCache() {
 9         PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
10         super.clearCache(principals);
11     }
原文地址:https://www.cnblogs.com/yinfengjiujian/p/9083361.html