学习笔记 : SpringBoot项目中Shiro集成EhCache
示例程序摘自 : 一个简单的SpringBoot整合Shiro的权限管理案例
添加依赖
Shiro-learning/spring boot project with shiro/springboot-shiro/pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependencies> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.4.1</version> </dependency> </dependencies>
|
创建EhCache缓存配置文件
Shiro-learning/spring boot project with shiro/springboot-shiro/src/main/resources/ehcache-shiro.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false" name="myEhcache"> <diskStore path="java.io.tmpdir"/> <cache name="authorizationCache" maxEntriesLocalHeap="2000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" statistics="true"> </cache> <cache name="authenticationCache" maxEntriesLocalHeap="2000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" statistics="true"> </cache> <cache name="activeSessionCache" maxEntriesLocalHeap="2000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" statistics="true"> </cache> <cache name="halfHour" maxElementsInMemory="10000" maxElementsOnDisk="100000" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" diskPersistent="false"/> <cache name="hour" maxElementsInMemory="10000" maxElementsOnDisk="100000" timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false" diskPersistent="false"/> <cache name="oneDay" maxElementsInMemory="10000" maxElementsOnDisk="100000" timeToIdleSeconds="86400" timeToLiveSeconds="86400" overflowToDisk="false" diskPersistent="false"/>
<defaultCache name="defaultCache" maxElementsInMemory="10000" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false" maxElementsOnDisk="100000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> </ehcache>
|
配置Shiro缓存
Shiro-learning/spring boot project with shiro/springboot-shiro/src/main/java/pers/huangyuhui/ss/shiro/ShiroConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
@Configuration public class ShiroConfig {
@Bean public SecurityManager securityManager(UserRealm userRealm, EhCacheManager ehCacheManager) { DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(); defaultWebSecurityManager.setRealm(userRealm); defaultWebSecurityManager.setCacheManager(ehCacheManager); return defaultWebSecurityManager; }
@Bean public EhCacheManager ehCacheManager() { CacheManager cacheManager = CacheManager.getCacheManager("myEhcache"); if (cacheManager == null) { cacheManager = CacheManager.create(); } EhCacheManager ehCacheManager = new EhCacheManager(); ehCacheManager.setCacheManager(cacheManager); return ehCacheManager; }
}
|
注意事项
如果使用的EhCache版本超过了2.5.0,那么<ehcache name="myEhcache">
的配置就显得非常重要了,不然EhCache会自动加载默认的名字 : _default_
,且EhCache2.5以后只允许创建单例的CacheManager(缓存管理器),所以应注意避免重复加载CacheManager哟 : 创建ehCacheManager的时先判断是否已存在cacheManager,没有的情况下再进行创建,判断的关键为ehcache-shiro.xml
(缓存配置文件)中配置的name
属性,可通过该属性来判断cacheManager是否加载了,示例代码如下 :
1 2 3 4
| CacheManager cacheManager = CacheManager.getCacheManager("myEhcache"); if (cacheManager == null) { cacheManager = CacheManager.create(); }
|
借鉴(非常感谢) : https://blog.csdn.net/Maslii/article/details/82380568