目标:让Shiro整合ehcache,提供缓存realm数据的功能。

1.引入encache配置文件,配置缓存

 <!-- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
--><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- 磁盘上的缓存的临时目录 ,默认是系统的临时目录,也可以手动指定一个目录-->
<diskStore path="java.io.tmpdir"/>
<!-- 默认的缓存区域的默认策略 -->
<!--
maxElementsInMemory:内存中元素最大存放的个数
eternal:缓存的对象是否永生不死。一般都是false。
timeToIdleSeconds:发呆的时间,多长时间不用,就干掉,秒
timeToLiveSeconds:存活的时间,活够了就干掉,秒
maxElementsOnDisk:硬盘上最大存放的元素的个数,如果内存10000个满了,就往硬盘上存。
memoryStoreEvictionPolicy:内存中存储和销毁元素的策略:默认使用LRU,解决的是缓存元素满了怎么办。
策略有三个:LRU、LFU、FIFO
LRU:最少使用被清理,次数
LFU:时间,闲置最长的时间
FIFO:管道策略,先进先出
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- Spring整合的菜单缓存 -->
<cache name="bos_menu_cache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
<!-- Shiro权限缓存-认证 -->
<cache name="bos_realm_authentication_cache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
<!-- Shiro权限缓存-授权 -->
<cache name="bos_realm_authorization_cache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>

2.引入坐标(其他坐标这里略)

 <!-- shiro整合ehcache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>

3.在Spring中配置shiro缓存

 <?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"> <!-- 配置Shiro核心Filter,bean的id必须和过滤器的名字一样 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 未认证,跳转到哪个页面 ,如果认证失败,跳转的默认页面 -->
<property name="loginUrl" value="/login.html" />
<!-- 登录页面页面,如果认证成功,则默认跳转的页面 -->
<property name="successUrl" value="/index.html" />
<!-- 如果没有授权,则默认跳转到该页面 -->
<property name="unauthorizedUrl" value="/unauthorized.html" />
<!-- shiro URL控制过滤器规则:配置的小过滤器链(过滤器栈):执行从上倒下有顺序 -->
<property name="filterChainDefinitions">
<value>
/login.html* = anon
/user_login.action* = anon
/validatecode.jsp* = anon
/css/** = anon
/js/** = anon
/images/** = anon
/services/** = anon
/pages/base/courier.html* = perms[courier:list]
/pages/base/area.html* = roles[base]
/** = authc
</value>
</property>
</bean> <!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="bosRealm"></property>
<!-- 开启Shiro缓存功能,需要在shiro安全管理器中注入shiro的 平台缓存管理器 -->
<property name="cacheManager" ref="shiroCacheManager" />
</bean> <!-- 配置Shiro的bean后处理器:用来初始化Shiro的bean在spring中-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 开启Shiro注解 -->
<!-- Enable Shiro Annotations for Spring-configured beans.
Only run after -->
<!-- the lifecycleBeanProcessor has run:
depends-on:当前bean初始化时,必须依赖于指定的bean,(指定的
bean必须先初始化)
下面的两个bean配置:传统的aop编程:增强、切点、切面
-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"/> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<!-- 必须注入安全管理器 -->
<property name="securityManager" ref="securityManager" />
</bean> <!-- shiro整合echcache的缓存配置 -->
<!-- 配置Shiro的平台缓存管理 -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!-- 注入ehcache的对象 -->
<property name="cacheManager" ref="ehCacheManager" />
</bean> </beans>

4.在reaml对象中指定缓存权限的数据的区域

 @Component("bosRealm")
public class BosRealm extends AuthorizingRealm{ //只需要向父类注入缓存区域即可
//认证缓存区域
@Value("bos_realm_authentication_cache")
//方法上注入按照参数注入,和方法名无关
public void setSuperAuthenticationCacheName(String authenticationCacheName){
super.setAuthenticationCacheName(authenticationCacheName);
}
//授权缓存区域
@Value("bos_realm_authorization_cache")
public void setSuperAuthorizationCacheName(String authorizationCacheName){
super.setAuthorizationCacheName(authorizationCacheName);
}

shiro整合ehcache的更多相关文章

  1. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  2. 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据

    一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...

  3. 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

    1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用c ...

  4. Shiro 整合SpringMVC 并且实现权限管理

    Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...

  5. Shiro 整合SpringMVC 并且实现权限管理,登录和注销

    Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...

  6. Shiro 整合SpringMVC 并实现权限管理,登录和注销

    Shiro 整合SpringMVC 并且实现权限管理,登录和注销 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring S ...

  7. Spring+Struts+Mybatis+Shiro整合配置

    Jar包

  8. apache shiro整合spring(一)

    apache shiro整合spring 将shiro配置文件整合到spring体系中 方式一:直接在spring的配置文件中import shiro的配置文件 方式二:直接在web.xml中配置sh ...

  9. Springboot + shiro 整合之Url拦截设置(转)

    shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.xml的配置文件: <span style="font-size:14px;" ...

随机推荐

  1. 如何修改nexus的端口号

    1. Maven仓库:放置所有JAR文件(WAR,ZIP,POM等等)的地方,所有Maven项目可以从同一个Maven仓库中获取自己所需要的依赖JAR,这节省了磁盘资源. 简言之,Maven仓库能帮助 ...

  2. Java常用类--数字常用类

    math java提供了基本的 + - * / %等基本算术运算的运算符,但对于更复杂的数学运算比如:三角函数,对数运算,指数运算就无能为力了.Java提供了Math工具类来完成这些复杂的运算,Mat ...

  3. android 弹起键盘把ui顶上去的解决办法

    键盘输入框上面的ui布局必须为Relative相对布局.然后设置 <activityandroid:name=".activity.HomeActivity"Android: ...

  4. Android自定义View的套路

    一.自定义View的流程 1.属性设置 在styles.xml中设置控件属性,如果你想直接harcode可以忽略这步 <!--name为声明的"属性集合"名,可以随便取,但是 ...

  5. JMeter基础教程1:若隐若现的参数化

    1. 什么是参数化? 在开始学习JMeter参数化之前,我们先了解下什么是参数化: 参数化是自动化测试脚本的一种常用技巧.简单来说,参数化的一般用法就是将脚本中的某些输入使用参数来代替,在脚本运行时指 ...

  6. 浅谈大型web系统架构(一)

    目录 Web前端系统 负载均衡系统 数据库集群系统 缓存系统 分布式存储系统 分布式服务器管理系统 代码发布系统   动态应用,是相对于网站静态内容而言,是指以c/c++.php.Java.perl. ...

  7. hdu 5909 Tree Cutting [树形DP fwt]

    hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...

  8. 记一次酷狗音乐API的获取,感兴趣的可以自己封装开发自己的音乐播放器

    1.本教程仅供个人学习用,禁止用于任何的商业和非法用途,如涉及版权问题请联系笔者删除. 2.随笔系作者原创文档,转载请注明文档来源:http://www.cnblogs.com/apresunday/ ...

  9. Windows Server 2016-安装AD域服务注意事项

    使用 Active Directory域服务 (AD DS) 服务器角色,可以创建用于用户和资源管理的可伸缩.安全及可管理的基础机构,并可以提供对启用目录的应用程序(如 Microsoft Excha ...

  10. asp.net core 中 sql server 2017 数据库连接测试

    使用sql server 2017 进行连接: 配置appsettings.json文件 { "ConnectionStrings": { "DefaultConnect ...