1、开发环境
①、spring开发环境配置
②、shiro相关开发环境配置
在此基础上再增加shiro的缓存组件包依赖
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.0</version> </dependency>
2、定义相应的缓存配置文件
<?xml version="1.1" encoding="UTF-8"?> <ehcache name="shirocache"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="2000" eternal="true" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"/> <cache name="passwordRetryCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="0" overflowToDisk="false"> </cache> <cache name="authorizationCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0" overflowToDisk="false"> </cache> <cache name="authenticationCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0" overflowToDisk="false"> </cache> <cache name="shiro-activeSessionCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0" overflowToDisk="false"> </cache> </ehcache>
在此配置文件之中实际上有以下几个核心选项:
“diskStore path=”java.io.tmpdir”:磁盘的存储目录; “name=”xxx””:对要进行缓存的项进行一个标注; “maxElementsInMemory=”2000”:可以缓存的最大的对象个数; “eternal=”false”:是否允许自动失效(如果某一个对象长时间不使用); “timeToIdleSeconds=”1800”:最小的失效时间,1800秒; “timeToLiveSeconds=”0”:最大的保存时间,单位是秒; “overflowToDisk=”false”:如果容量过多,可以将其保存在磁盘
3、如果要想使缓存生效,则还需要修改applicationContext.xml文件进行缓存配置:
定义缓存管理器:
<!-- 进行缓存的操作配置 --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> </bean>
在安全管理器之中注册此缓存管理器:
<!-- 配置SecurityManager的管理 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- 配置你需要使用的Realms --> <property name="realm" ref="memberRealm"/> <property name="cacheManager" ref="cacheManager"/> </bean>