目标:让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. webpack 介绍 & 安装 & 常用命令

    webpack 介绍 & 安装 & 常用命令 webpack系列目录 webpack 系列 一:模块系统的演进 webpack 系列 二:webpack 介绍&安装 webpa ...

  2. python字符串问题

    相关知识点: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unico ...

  3. 解决C#编译中"csc不是内部或外部命令"的问题

    安装完 VisualStudio 编译环境后,是不能用命令行直接编译写好的csc文件的,如果不配置环境变量,在命令提示符(cmd)中编译扩展名为cs的文件,会出现错误提示"csc不是内部或外 ...

  4. DELL服务器硬件信息采集SHELL脚本

    DELL服务器硬件信息采集SHELL脚本最近需要做资产列表,要采集DELL服务器的硬件信息,包括如下信息:1.操作系统信息(类型,版本,内核,平台,主机名)2.主板信息(厂商,机型,序列号)3.CPU ...

  5. samephore()信号量跨线程通信

    samephore1: #include <stdio.h> #include <stdlib.h> #include <Windows.h> ] = " ...

  6. Python之Django rest_Framework框架源码分析

    #!/usr/bin/env python # -*- coding:utf-8 -*- from rest_framework.views import APIView from rest_fram ...

  7. VBA小技巧

    运用VBA时,可以构造一些函数去实现诸如printf的方便函数. Public Function printf(mask As String, ParamArray tokens()) As Stri ...

  8. mysql 删匿名帐户

    mysql默认安装,带有匿名帐户 删除它很简单 登录进入msyql并执行以下三行语句: use mysql; delete from user where user=''; flush privile ...

  9. MySQL基本应用

    1.默认类型转换 CREATE TABLE `indextest` (`id` int(10)  AUTO_INCREMENT,`name` varchar(10) DEFAULT NULL, PRI ...

  10. 树莓派小车(三)Python控制小车

    正文之前 由于最近忙于复习赶考,所以暂时没有拿起树莓派小车,直到昨天,终于空出时间来把代码整理一下来和大家分享. 正文 在树莓派小车系列之二中,讲到了树莓派的引脚定义方式有两种: PHYSICAL N ...