zoukankan      html  css  js  c++  java
  • 【原创】大叔问题定位分享(35)spring中session失效时间

    spring项目中将sessionid对应的cookie过期时间设置很长,但是实际session还是在半个小时后失效,跟了一下代码,spring中session实现接口为

    org.springframework.session.SessionRepository

    public interface SessionRepository<S extends Session> {
        S createSession();
    
        void save(S var1);
    
        S findById(String var1);
    
        void deleteById(String var1);
    }

    这个接口有两个实现类:

    MapSessionRepository
    RedisOperationsSessionRepository

    单机环境使用前者,分布式环境使用后者,来看后者代码:

    org.springframework.session.data.redis.RedisOperationsSessionRepository

        public RedisOperationsSessionRepository.RedisSession createSession() {
            RedisOperationsSessionRepository.RedisSession redisSession = new RedisOperationsSessionRepository.RedisSession();
            if(this.defaultMaxInactiveInterval != null) {
                redisSession.setMaxInactiveInterval(Duration.ofSeconds((long)this.defaultMaxInactiveInterval.intValue()));
            }
    
            return redisSession;
        }

    org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession

            RedisSession() {
                this(new MapSession());
                this.delta.put("creationTime", Long.valueOf(this.getCreationTime().toEpochMilli()));
                this.delta.put("maxInactiveInterval", Integer.valueOf((int)this.getMaxInactiveInterval().getSeconds()));
                this.delta.put("lastAccessedTime", Long.valueOf(this.getLastAccessedTime().toEpochMilli()));
                this.isNew = true;
                this.flushImmediateIfNecessary();
            }
    
            public boolean isExpired() {
                return this.cached.isExpired();
            }

    org.springframework.session.MapSession

        public boolean isExpired() {
            return this.isExpired(Instant.now());
        }
    
        boolean isExpired(Instant now) {
            return this.maxInactiveInterval.isNegative()?false:now.minus(this.maxInactiveInterval).compareTo(this.lastAccessedTime) >= 0;
        }

    可见是在创建session的时候设置两个时间,

    lastAccessedTime
    maxInactiveInterval

    如果 当前时间 - maxInactiveInterval > lastAccessedTime 就会认为session过期,设置的方法:

    @EnableRedisHttpSession(maxInactiveIntervalInSeconds=2000) 

  • 相关阅读:
    HDU 4315 Climbing the Hill [阶梯Nim]
    POJ 1704 Georgia and Bob [阶梯Nim]
    BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏 [Nim游戏 SG函数]
    BZOJ 1299: [LLH邀请赛]巧克力棒 [组合游戏]
    浏览器缓存知识点总结
    软件测试自动化的最新趋势
    性能测试面试题(附答案
    最流行的自动化测试工具,总有一款适合你
    49种软件测试方法
    linux执行jmeter脚本解决响应数据为空
  • 原文地址:https://www.cnblogs.com/barneywill/p/11163735.html
Copyright © 2011-2022 走看看