tao1027879132
tao1027879132
2015-08-20 02:08
采纳率: 100%
浏览 23.6k
已采纳

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条回答 默认 最新

  • tao1027879132
    tao1027879132 2016-08-08 03:00
    已采纳

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

    点赞 评论
  • aib628
    aib628 2015-10-11 14:38

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

    点赞 评论
  • tao1027879132
    tao1027879132 2016-08-08 02:56

    图片说明

    点赞 评论
  • tao1027879132
    tao1027879132 2016-08-08 02:56

    ~~~~没想到这么多人遇到这个问题,前两天又朋友加到了我的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-->


    点赞 评论
  • tao1027879132
    tao1027879132 2016-08-08 02:57




    <!--ehcache-->


    点赞 评论
  • tao1027879132
    tao1027879132 2016-08-08 02:58
    配置粘不上来  截图了~~
    ![图片说明](https://img-ask.csdn.net/upload/201608/08/1470625081_276426.png)
    
    点赞 评论
  • tao1027879132
    tao1027879132 2017-01-03 09:22

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

    点赞 评论
  • kevincfb
    kevincfb 2018-03-02 05:40

    用的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>
    
    
    点赞 评论

相关推荐