Shiro Cache配置

本文介绍了如何配置Shiro使用Ehcache作为缓存管理器,强调了在系统已配置hibernate二级缓存时如何解决冲突,并提供了通过JMX监控Ehcache的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要实现Shiro的Cache配置非常简单,默认使用ehcache,直接配置cacheManager制定实现为EhCacheManager,然后通过property制定对应的ehcache-shiro.xml文件

spring配置文件
<!-- 用户授权信息Cache, 采用EhCache -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />
    </bean>

    <!-- Shiro's main business-tier object for web-enabled applications -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userRealm"/>
        <property name="sessionManager" ref="sessionManager"/>
        <property name="cacheManager" ref="cacheManager"/>
        <property name="rememberMeManager" ref="rememberMeManager"/>
    </bean>

然后在ehcache-shiro.xml文件中,配置需要使用的Cache相关信息。

Shiro ehcache配置xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es_shiro" updateCheck="false">

    <diskStore path="java.io.tmpdir"/>

    <!-- 登录记录缓存 锁定10分钟 -->
    <cache name="passwordRetryCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <cache name="authorizationCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <cache name="authenticationCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <cache name="shiro-activeSessionCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

</ehcache>

这里有2个要注意的问题:
1. 如果系统使用了hibernate,hibernate配置了singleton secondary layer cache,那么会导致shiro的cache配置文件不被load的情况,我的解决方案是把singleton改成允许多个cache实例。
2. 如果想查看cache是否生效,使用情况,可以通过jmx来export相关ehcache统计信息。


    <bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="beans">
            <map>
                <entry key="com.cloud.DemoSystem:name=ehcacheStatisticsMBean"
                    value-ref="ehCacheMBeanRegistration" />              
            </map>
        </property>
         <property name="server" ref="mbeanServer"/>
    </bean>

    <!-- JMX for ehcache -->
     <bean id="ehCacheMBeanRegistration"
                class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

                <property name="staticMethod"
                        value="net.sf.ehcache.management.ManagementService.registerMBeans"/>
                <property name="arguments">
                        <list>
                                <ref bean="cacheManager"/>
                                <ref bean="mbeanServer"/>
                                <value>true</value>
                                <value>true</value>
                                <value>true</value>
                                <value>true</value>
                        </list>
                </property>
        </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值