010-shiro与spring web项目整合【四】缓存Ehcache
2023-09-14 09:08:48 时间
一、Ehcache
shiro每次授权都会通过realm获取权限信息,为了提高访问速度需要添加缓存,第一次从realm中读取权限数据,之后不再读取,这里Shiro和Ehcache整合。
1、添加Ehcache的jar包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.2</version> </dependency>
2、配置cacheManager
在applicationContext-shiro.xml中配置缓存管理器。
<!-- securityManager安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="customRealm"/> <!-- 注入缓存管理器 --> <property name="cacheManager" ref="cacheManager"/> <!-- 注入session管理器 --> <property name="sessionManager" ref="sessionManager"/> <!-- 记住我 --> <property name="rememberMeManager" ref="rememberMeManager"/> </bean> <!-- 缓存管理器 --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/> </bean>
3、配置shiro-ehcache.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!--diskStore:缓存数据持久化的目录 地址 --> <diskStore path="F:\develop\ehcache" /> <defaultCache maxElementsInMemory="1000" maxElementsOnDisk="10000000" eternal="false" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> </ehcache>
4、清空缓存
当用户权限修改后,用户再次登陆shiro会自动调用realm从数据库获取权限数据,如果在修改权限后想立即清除缓存则可以调用realm的clearCache方法清除缓存。
realm中定义clearCached方法:
//清除缓存 public void clearCached() { PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals(); super.clearCache(principals); }
在权限修改后调用realm中的方法,realm已经由spring管理,所以从spring中获取realm实例,调用clearCached方法。
相关文章
- spring项目(springmvc)(多模块/单模块)maven打包引入第三方jar方式,使用scope:system配置systemPath编译,不用添加到本地仓库!
- springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
- CXF Spring 实现WebService - 观千剑而后识器,操千曲而后晓声。 - 博客频道
- intellij idea 2021.2:为一个spring boot项目改名
- MongoDB初体验:Spring中使用MongoTemplate查询数据
- Spring boot后台搭建二集成Shiro权限控制
- Spring中配置和读取多个Properties文件--转
- Spring+Quartz实现定时执行任务的配置
- Java Spring源码研究之BeanNameUrlHandlerMapping
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- 【项目实战】Spring IoC 容器介绍
- Spring自带工具类(断言、ObjectUtils、FileCopyUtils、ResourceUtils、StreamUtils、ReflectionUtils、AopUtils、AopCont)
- spring boot + vue 前后端分离时间戳转换为 yyyy:MM:dd HH:mm:ss格式
- spring security 关于 http.sessionManagement().maximumSessions(1);的探究
- spring boot+vue前后端项目的分离(我的第一个前后端分离项目)
- Spring MVC 学习总结(八)——Spring MVC概要与环境配置(IDEA+Maven+Tomcat7+JDK8、示例与视频)
- spring+websocket综合(springMVC+spring+MyBatis这是SSM框架和websocket集成技术)
- Spring之对象依赖关系(依赖注入Dependency Injection)
- 012-Spring Boot web【一】web项目搭建、请求参数、RestController、使用jsp、freemarker,web容器tomcat和jetty
- 采用CXF+spring+restful创建一个web接口项目