shiro配置ehcache缓存
由于使用shiro配置缓存,查了好多资料,都不明确,搞得配置起来很费时间,特此整理正确的配置如下,供各位参考,希望能够帮助大家。
首先,在resource下建立ehcache.xml(名字可换)
然后在这个配置文件中进行配置:
<ehcache>
<!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
<diskStore path="java.io.tmpdir"/>
<!-- 设定缓存的默认数据过期策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
</ehcache>
第二步,在pom文件中加入依赖
必须导入shiro-ehcache、ehcache依赖,因为在配置shiro缓存时,不依赖shiro-ehcache,找不到EhCacheManager
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-ehcache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
其他的shiro依赖不再赘述。
第三步,配置spring-shiro.xml文件进行缓存配置
如下:
<!-- 缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
</bean>
<bean id="sManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
<!-- 注入缓存管理器 -->
<property name="cacheManager" ref="cacheManager"/>
</bean>
这样配置完,shiro已经可以使用默认的缓存了。
更加详细深入的配置后期更新。