shiro教程:整合ehcache缓存
这个是在ssm的基础上再去整合shiro和ehcache的,整合ehcache主要是为了减少后台shiro拦截的次数,因为如果我们不使用缓存的话,后台shiro的认证和授权的拦截器就会反复的进行拦截,导致系统的运行效率不高,因此使用缓存是一种很好的解决的方法,下面我们看看如何整合ehcache。
1、加入jar包pom.xml
在这之前,我们先加入shiro和ehcache的相关jar包
<!-- shiro --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><exclusions><exclusion><artifactId>slf4j-api</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><exclusions><exclusion><artifactId>slf4j-api</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency>
2、配置ehcache.xml配置文件
在整合之前,我们需要配置一下ehcache的一些参数,设置好ehcache的环境。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCache"><diskStore path="C:\shiro\ehcache" />
<!-- <diskStore path="java.io.tmpdir"/> --><!-- eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。 maxElementsInMemory:缓存中允许创建的最大对象数 overflowToDisk:内存不足时,是否启用磁盘缓存。 timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前, 两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。 timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。 memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。 diskPersistent:设定在虚拟机重启时是否进行磁盘存储,默认为falsediskExpiryThreadIntervalSeconds: 属性可以设置该线程执行的间隔时间(默认是120秒,不能太小1 FIFO,先进先出 2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。 3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。 --> <defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"/><cache name="activeSessionCache"maxElementsInMemory="10000"eternal="true"overflowToDisk="false"diskPersistent="true"diskExpiryThreadIntervalSeconds="600"/><cache name="shiro.authorizationCache"maxElementsInMemory="100"eternal="false"timeToLiveSeconds="600"overflowToDisk="false"/></ehcache>
3、整合spring
整合spring其实很简单的,以前总觉得是一个很复杂的事情一样,每次什么都要整合spring,其实spring就是一个容器,再说的直白点,就是一个map类似的容器,然后通过对容器的操作来实现,因此,如果我们需要让spring来管理ehcache的话,我们只是需要将ehcache的配置文件交给spring来管理即可。
下面就是spring的配置文件applicationContext-ehcache.xml
的ehcache的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd "><!-- 安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="userRealm" /><property name="cacheManager" ref="cacheManager"/></bean><!-- 缓存管理器 --><bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/></bean>
</beans>
4、缓冲清空
当用户权限修改后,用户再次登陆shiro会自动调用realm从数据库获取权限数据,如果在修改权限后想立即清除缓存则可以调用realm的clearCache方法清除缓存。
realm中定义clearCached方法:
@Component
public class ShiroDBRealm extends AuthorizingRealm {/*** * @Description: 权限修改生效后,立即刷新清空缓存,则可以实现用户不退出生效新的权限* * @author sihai* @date 2017年9月29日 下午9:34:07*/public void clearCache() {PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();super.clearCache(principals);}
}
5、测试是否整合成功
@Component
public class ShiroDBRealm extends AuthorizingRealm {@Autowiredprivate UserService userService; public ShiroDBRealm(CacheManager cacheManager) {super(cacheManager);}/*** 用于登录认证*/@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// 1. 从token中获取用户在表单中输入的域,即 用户名 以及 密码,以此来和数据库中的用户真实数据进行匹配UsernamePasswordToken userToken = (UsernamePasswordToken)token;String username = userToken.getUsername(); // 用户的登录名String password = String.valueOf(userToken.getPassword()); // 用户的密码// 2. 根据用户输入的用户名和数据库进行匹配,查询数据库中的用户,如果查询不到,则返回一个nullSysUser user = userService.queryUserByUsername(username);// 3. 判断数据库中查询出来的用户是否存在,不存在代表用户名密码错误;如果存在,则返回 AuthenticationInfoif (user == null) {return null;}String dbPassword = user.getPassword();String dbSalt = user.getAuthSalt();String userPassword = ShiroPasswordUtil.getShiroPassword(password, dbSalt, 2);if (!userPassword.equals(dbPassword)) {// 抛出一个异常,密码不正确throw new IncorrectCredentialsException();}ActiveUser activeUser = new ActiveUser();activeUser.setUserId(user.getId());activeUser.setUsername(user.getUsername());// 4. 返回AuthenticationInfo// 参数意义:// Object principal: 用户对象,可以使一个对象类,或者一个字符串,存与session中 // Object credentials:密码 // 题外话:我们目前直接根据用户名密码来查询,所以这里直接放用户输入的密码即可;但是,也可疑直接用用户名来查询数据库中的用户,再然后进行密码的比对,如此则此处应该填写用户输入的密码// String realmName:当前我们自定义realm的名称AuthenticationInfo info = new SimpleAuthenticationInfo(activeUser, password, getName());return info;}/*** 用于授权鉴权*/@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {// 从principals中获取当前用户ActiveUser sessionUser = (ActiveUser)principals.getPrimaryPrincipal();String userid = sessionUser.getUserId();// 模拟从数据库中获取用户的权限(资源权限字符串)List<SysPermission> permissionList = null;try {permissionList = userService.findPermissionListByUserId(userid);} catch (Exception e) {e.printStackTrace();}List<String> percodeList = new ArrayList<String>();for (SysPermission p : permissionList) {percodeList.add(p.getPercode());}SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();simpleAuthorizationInfo.addStringPermissions(percodeList);return simpleAuthorizationInfo;}
}
上面登录认证和授权的代码,当我们没有加入ehcache缓存的时候,我们前台每次授权认证的时候,我们都会访问这段代码,如果当我们只会第一次访问,后面不再访问的时候,说明整合成功了。
shiro教程:整合ehcache缓存相关推荐
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- Shiro教程,整合SpringBoot项目实战(笔记)
1.shiro 1.1什么是权限管理 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己 ...
- springboot mybatis 项目框架源码 shiro 集成代码生成器 ehcache缓存
1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器) freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块 ...
- Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架
Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...
- Spring MVC整合Ehcache缓存框架
https://blog.csdn.net/u012562943/article/details/52289433 转载于:https://www.cnblogs.com/pjlhf/p/881874 ...
- springboot整个缓存_springboot整合ehcache缓存
{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...
- ehcache 冲突_解决Ehcache缓存警告问题
警告: Creating a new instance of CacheManager using the diskStorePath "D:\Apache Tomcat 6.0.18\te ...
- Spring Boot 2.x基础教程:使用EhCache缓存集群
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 上一篇我们介绍了在Spring Boot中整合EhCac ...
- SpringBoot整合(四)整合Ehcache、Redis、Memcached、jetcache、j2cache缓存
企业级应用主要作用是信息处理,当需要读取数据时,由于受限于数据库的访问效率,导致整体系统性能偏低. 为了改善上述现象,开发者通常会在应用程序与数据库之间建立一种临时的数据存储机制,该区域中的数 ...
最新文章
- BZOJ4766: 文艺计算姬
- Server-U的批量用户创建
- python元组的定义方式_序列之元组详解
- libc-glibc
- 科大星云诗社动态20210524
- .NetCore之下载文件
- 将button变成圆形(有弧度)
- 第四次作业——测试作业
- html5移动端开发(rem和媒体查询@media)
- python抓包教程_Python Charles抓包配置实现流程图解
- Invalid prop: type check failed for prop “index“. Expected String with value “145“...
- 斐波那契数列c语言while,C语言数据结构递归之斐波那契数列
- windows微信双开
- python中检测键盘(上下左右) 代码
- opengl 画椭圆_椭圆围城与圆型观光步道
- Jetson TX2零基础学习(一)——连线、刷机
- 【CISSP备考笔记】第2章:资产安全
- VCC,VDD,VSS,VEE区别
- 低风险整体式微服务演进第二部分
- iOS开发之资料收集