shiro和spring集成时session管理器超时时间问题

这是我的配置文件,我配置了并发人数控制和动态权限过滤,然后session超时时间这里也是配置了的,然后并没有什么鸟用,在登录以后获取超时时间也是正常的,但还是1分钟就过期了。

<?xml version="1.0" encoding="UTF-8"?>
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="shiroFilter"/>
    <property name="targetMethod" value="setFilterChainResolver"/>
    <property name="arguments" ref="filterChainResolver"/>
</bean>
<bean id="filterChainResolver"   class="com.sfkj.platform.shiro.CustomPathMatchingFilterChainResolver">  
    <property name="customDefaultFilterChainManager" ref="filterChainManager"/>  
</bean>  
<bean id="filterChainManager"    class="com.sfkj.platform.shiro.CustomDefaultFilterChainManager">  
    <property name="loginUrl" value="/index/redirect.html"/>  
    <property name="successUrl" value="/index/redirect.html"/>  
    <property name="unauthorizedUrl" value="/index/unauthorizedUrl.html"/>  
    <property name="customFilters">  
        <util:map>  
            <entry key="kickout" value-ref="kickoutSessionControlFilter"/> 
            <entry key="sysUser" value-ref="sysUserFilter"/>
        </util:map>  
    </property>  
    <property name="defaultFilterChainDefinitions">  
        <value>  
            <!-- /login = authc  
            /logout = logout  
            /unauthorized.jsp = authc  
            /** = user,sysUser  --> 
            /admin/**=kickout
            /** =anon
        </value>  
    </property>  
</bean>  
 <bean id="sysUserFilter" class="com.sfkj.platform.shiro.SysUserFilter"/>
<bean id="kickoutSessionControlFilter"   class="com.sfkj.platform.shiro.KickoutSessionControlFilter">  
    <property name="cacheManager" ref="cacheManager"/>  
    <property name="sessionManager" ref="sessionManager"/>  
    <property name="kickoutAfter" value="false"/>  
    <property name="maxSession" value="1"/>  
    <property name="kickoutUrl" value="/index/kickoutUrl.html"/>  
</bean> 
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager" />
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="userRealm" />
    <property name="cacheManager" ref="cacheManager" />
    <property name="sessionManager" ref="sessionManager"/>
</bean>
<bean id="userRealm" class="com.sfkj.platform.shiro.UserRealm"/> 
 <!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManager" ref="ehCacheManager"/>
    <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean>
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

  <!-- 会话管理器 -->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    <property name="globalSessionTimeout" value="604800000"/>
    <property name="deleteInvalidSessions" value="true"/>
    <property name="sessionValidationSchedulerEnabled" value="true"/>
    <!--  <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>  -->
    <property name="sessionDAO" ref="sessionDAO"/>
    <property name="sessionIdCookieEnabled" value="true"/>
    <property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>
<!-- 会话验证调度器 -->
<!--<bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
    <property name="sessionValidationInterval" value="1800000"/>
    <property name="sessionManager" ref="sessionManager"/>
</bean> -->
<!-- 会话DAO -->
<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
    <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
    <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
</bean>
<!-- 会话ID生成器 -->
<bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>
 <!-- 会话Cookie模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
    <constructor-arg value="sid"/>
    <property name="httpOnly" value="true"/>
    <property name="maxAge" value="-1"/>
</bean>

 <!-- Shiro生命周期处理器-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>




maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="604800"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">

maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="604800"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">

maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="604800"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">

maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="604800"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">


8个回答

具体是什么原因我还没找到,希望知道的朋友说一声 谢谢 qq就是我的名字 大家相互讨论

问题详解:http://blog.sina.com.cn/s/blog_d06cca100102x3xa.html qq不再对此问题添加好友

我也是遇到了这个问题,登录2分钟如果不动页面就丢失Session了,请问是怎么解决的呢?如果一直在页面操作则不会丢失

~~~~没想到这么多人遇到这个问题,前两天又朋友加到了我的qq·~ 花了点时间定位了问题
解决办法是:
将缓存管理器封装到了spring中来管理
图片说明
SpringCacheManagerWrapper:
/**

  • 包装Spring cache抽象
  • User: Zhang Kaitao

  • Date: 13-3-23 上午8:26

  • Version: 1.0
    */
    public class SpringCacheManagerWrapper implements CacheManager {

    private org.springframework.cache.CacheManager cacheManager;

    /**

    • 设置spring cache manager *
    • @param cacheManager */ public void setCacheManager(org.springframework.cache.CacheManager cacheManager) { this.cacheManager = cacheManager; }

    @SuppressWarnings("unchecked")
    @Override
    public Cache getCache(String name) throws CacheException {
    org.springframework.cache.Cache springCache = cacheManager.getCache(name);
    return new SpringCacheWrapper(springCache);
    }

    @SuppressWarnings("rawtypes")
    static class SpringCacheWrapper implements Cache {
    private org.springframework.cache.Cache springCache;

    SpringCacheWrapper(org.springframework.cache.Cache springCache) {
        this.springCache = springCache;
    }
    
    @Override
    public Object get(Object key) throws CacheException {
        Object value = springCache.get(key);
        if (value instanceof SimpleValueWrapper) {
            return ((SimpleValueWrapper) value).get();
        }
        return value;
    }
    
    @Override
    public Object put(Object key, Object value) throws CacheException {
        springCache.put(key, value);
        return value;
    }
    
    @Override
    public Object remove(Object key) throws CacheException {
        springCache.evict(key);
        return null;
    }
    
    @Override
    public void clear() throws CacheException {
        springCache.clear();
    }
    
    @Override
    public int size() {
        if(springCache.getNativeCache() instanceof Ehcache) {
            Ehcache ehcache = (Ehcache) springCache.getNativeCache();
            return ehcache.getSize();
        }
        throw new UnsupportedOperationException("invoke spring cache abstract size method not supported");
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public Set keys() {
        if(springCache.getNativeCache() instanceof Ehcache) {
            Ehcache ehcache = (Ehcache) springCache.getNativeCache();
            return new HashSet(ehcache.getKeys());
        }
        throw new UnsupportedOperationException("invoke spring cache abstract keys method not supported");
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public Collection values() {
        if(springCache.getNativeCache() instanceof Ehcache) {
            Ehcache ehcache = (Ehcache) springCache.getNativeCache();
            List keys = ehcache.getKeys();
            if (!CollectionUtils.isEmpty(keys)) {
                List values = new ArrayList(keys.size());
                for (Object key : keys) {
                    Object value = get(key);
                    if (value != null) {
                        values.add(value);
                    }
                }
                return Collections.unmodifiableList(values);
            } else {
                return Collections.emptyList();
            }
        }
        throw new UnsupportedOperationException("invoke spring cache abstract values method not supported");
    }
    

    }
    }

springCacheManager 配置:



<!--ehcache-->


licheng1030
疯狂的CtrlC 回复7-风中追风: 这个问题解决了么?
3 年多之前 回复
yuan1990731
7-风中追风 cacheManager.getCache(name) 取到的org.springframework.cache.Cache springCache 为空 什么原因?
3 年多之前 回复
qq_35891283
qq_35891283 这个亲测方法可行。
3 年多之前 回复

用的1.2.3的,按照你上面的spring-shiro.xml 和 Ehcache.xml配置,解决了。

代号如下:

 <!-- 缓存管理器 使用Ehcache实现 -->
    <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <!--classpath是缓存属性的配置文件  -->    
        <!--<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />-->
        <property name="cacheManager" ref="ehCacheManager"/>
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
    </bean>
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="cyberRealm"/>
         <!--注入缓存管理器  -->  
        <property name="cacheManager" ref="shiroEhcacheManager" />
        <property name="sessionManager" ref="sessionManager"/>
    </bean>

    <bean id="cyberRealm" class="com.porseidon.shiro.AdminLTERealm">
         <property name="credentialsMatcher" ref="hashedCredentialsMatcher" />  
         <property name="cacheManager" ref="shiroEhcacheManager"/>
         <!-- 启用身份验证缓存,即缓存AuthenticationInfo信息,默认false -->
         <property name="authenticationCachingEnabled" value="false"/>
         <!-- 缓存AuthenticationInfo信息的缓存名称 -->
         <property name="authenticationCacheName" value="authenticationCache" />
          <!-- 启用身份验证缓存,即缓存AuthenticationInfo信息,默认false -->
         <property name="authorizationCachingEnabled" value="false" />
         <!-- 缓存authorizationCache信息的缓存名称 -->
         <property name="authorizationCacheName" value="authorizationCache" />
    </bean>

    <!-- 密码匹配规则 -->
    <bean id="hashedCredentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">  
        <property name="hashAlgorithmName" value="MD5" />  
        <property name="storedCredentialsHexEncoded" value="true" />  
        <property name="hashIterations" value="1" />  
    </bean>  

    <!-- 会话管理器 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <!-- 超时时间 这里是7天 -->
        <property name="globalSessionTimeout" value="604800000"/>
        <property name="sessionDAO" ref="sessionDAO"/>
        <property name="sessionIdCookie" ref="sessionIdCookie"/>
        <!-- 定时检查失效的session -->
        <property name="sessionValidationSchedulerEnabled" value="true" />

        <property name="deleteInvalidSessions" value="true"/>
        <!--  <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>  -->
        <property name="sessionIdCookieEnabled" value="true"/>
    </bean>

     <!-- 会话DAO 用于会话的CRUD -->
    <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <!-- Session缓存名字,默认就是shiro-activeSessionCache -->
        <property name="activeSessionsCacheName" value="activeSessionCache"/>
        <property name="cacheManager" ref="shiroEhcacheManager"/>
    </bean>

    <!-- 会话Cookie模板 -->
    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg value="sid"/>
        <property name="httpOnly" value="true"/>
        <property name="maxAge" value="-1"/>
    </bean>




<!--ehcache-->


配置粘不上来  截图了~~
![图片说明](https://img-ask.csdn.net/upload/201608/08/1470625081_276426.png)

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问