开发者社区 问答 正文

shiro 在使用shiro时候配置的ehcache.xml不起作用 配置报错

    <!-- 设定shiro的权限管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--设置自定义realm -->
        <property name="realm" ref="monitorRealm" />
        <!-- 设置缓存管理器 -->
        <property name="cacheManager" ref="ehCacheManager" />
        <!-- 有次设置才能使得将 认证 授权信息交给 上面的cachemanager来管理-->
        <property name="sessionMode" value="native" />
        <!-- 设置session管理器 -->
        <property name="sessionManager" ref="sessionManager" />
        <!-- 设置remember me 管理器 -->
        <property name="rememberMeManager" ref="rememberMeManager"/>
    </bean>

    <!--自定义Realm 继承自AuthorizingRealm 加载用户信息和权限信息-->
    <bean id="monitorRealm" class="main.com.essence.common.security.service.impl.MonitorRealmImpl"></bean>

    
    <!-- shiro的session管理器 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <property name="globalSessionTimeout" value="50000"/>
        <property name="deleteInvalidSessions" value="true"/>
        <property name="sessionValidationSchedulerEnabled" value="true"/>
        <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
        <property name="sessionDAO" ref="sessionDAO" />
    </bean>
    <!-- 会话验证调度器 -->
    <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
        <property name="sessionValidationInterval" value="1800000"/>
        <property name="sessionManager" ref="sessionManager"/>
    </bean>

    <!-- shiro session由这个缓存来处理 -->
    <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
    </bean>
    
    <bean id="ehCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
           <property name="cacheManager" ref="cacheManager"/>
           <property name="cacheManagerConfigFile" value="classpath:/main/resources/ehcache.xml"/>

    </bean>

在ehcache.xml中

   <cache name="shiro-activeSessionCache"  
           maxElementsInMemory="3000"  
           eternal="false"  
           overflowToDisk="true"  
           timeToIdleSeconds="30"  
           timeToLiveSeconds="30"  
            />
    <cache name="userCache"  
           maxElementsInMemory="10000"  
           eternal="true"  
           overflowToDisk="true"
           timeToIdleSeconds="0"  
           timeToLiveSeconds="0"  
            /> 

<!-- shiro session由这个缓存来处理 -->
    <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
    </bean>中value=shiro-activeSessionCache   根本没作用啊   在ehcache.xml中配置的这个cache没起作用   反而是

<!-- shiro的session管理器 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <property name="globalSessionTimeout" value="50000"/>
        <property name="deleteInvalidSessions" value="true"/>
        <property name="sessionValidationSchedulerEnabled" value="true"/>
        <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
        <property name="sessionDAO" ref="sessionDAO" />
    </bean>中的设置失效时间起作用了.....



还有个问题就是我用cacheManager中取得userCache   可以取到,但是在xml中的配置是不起作用的   求大神们知道      在线等候..........................

展开
收起
huc_逆天 2020-05-28 09:21:50 473 分享 版权
阿里云 AI 助理回答

从你的描述来看,你遇到的问题主要涉及Ehcache的配置不生效以及通过CacheManager获取缓存时,XML配置未按预期工作。下面我将尝试为你提供一些可能的解决方案和排查思路:

Ehcache配置不生效

  1. 检查配置文件路径:确保ehcache.xml文件被正确地放置在了应用程序的类路径(classpath)下,这样Ehcache才能在启动时自动加载它。

  2. 命名空间问题:确认你的ehcache.xml是否包含了正确的Ehcache命名空间声明,例如:

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    
  3. 属性理解

    • eternal="true"表示该缓存项永不过期,所以如果设置了这个属性,timeToIdleSecondstimeToLiveSeconds将不起作用。
    • overflowToDisk="true"意味着当内存中的元素超过maxElementsInMemory限制时,会溢写到磁盘上。请确认你的应用有足够的权限操作磁盘,并且配置了合适的磁盘存储路径(虽然这通常不是配置不生效的直接原因)。
  4. 缓存名称匹配:确保你在代码中通过CacheManager获取缓存实例时使用的名称与XML中定义的缓存名称完全一致。例如,如果你在XML中定义的是<cache name="shiro-activeSessionCache">,那么获取缓存时也应使用这个名字。

  5. 依赖冲突或版本问题:有时候,项目中可能存在多个版本的Ehcache库或者与其他缓存库有冲突,导致配置解析异常。检查项目的依赖管理,确保只有一份Ehcache并且是兼容的版本。

CacheManager取得Cache配置无效

  • 重新初始化问题:如果你的应用程序在运行时动态修改了Ehcache配置,但没有重新初始化CacheManager或对应的Cache实例,那么新的配置不会生效。确保在更改配置后,适当地重新加载或重建CacheManager。

  • 日志分析:开启更详细的日志记录,特别是Ehcache相关的日志,可以帮助你了解配置加载过程中的任何错误或警告信息,从而定位问题所在。

  • 环境差异:确认在不同的运行环境下(如开发、测试、生产)配置行为是否一致,有时环境差异也会导致配置不生效。

如果以上建议仍不能解决问题,建议提供更详细的错误信息或日志,以便进行更深入的分析。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答标签:
问答地址: