shiro整合ehcache

目标:让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>
View Code

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>
View Code

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>
View Code

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     }
View Code

 

转载于:https://www.cnblogs.com/lichangyun/p/8425362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值