博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【原创】大叔问题定位分享(35)spring中session失效时间
阅读量:4982 次
发布时间:2019-06-12

本文共 2006 字,大约阅读时间需要 6 分钟。

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

org.springframework.session.SessionRepository

public interface SessionRepository {    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) 

 

转载于:https://www.cnblogs.com/barneywill/p/11163735.html

你可能感兴趣的文章
[Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
查看>>
[Angular 8] Implement a Custom Preloading Strategy with Angular
查看>>
[Dart] Understand Classes and Inheritance in Dart
查看>>
[Angular] Using Pipe for function memoization
查看>>
[Angular 8] Custom Route Preloading with ngx-quicklink and Angular
查看>>
[Angular 8] Calculate and Measure Performance budgets with the Angular CLI
查看>>
[CSS] Conditionally Assign Style to a Parent Element with Focus-Within Pseudo-class
查看>>
[React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks
查看>>
[Functional Programming] Rewrite a reducer with functional state ADT
查看>>
[Dart] Manipulate Lists/Arrays in Dart
查看>>
[AngularJS] Extend Controller
查看>>
[CSS] The :empty Pseudo Selector Gotchas
查看>>
[Cypress] install, configure, and script Cypress for JavaScript web applications -- part5
查看>>
[Flutter] Layout
查看>>
[RxJS] Convert a Node.js style callback to Observable: bindNodeCallback
查看>>
[NgRx] Setting up NgRx Router Store and the Time-Travelling Debugger
查看>>
[ARIA] Accessible modal dialogs
查看>>
[Dart] Dynamic variable in Dart
查看>>
[ARIA] Accessible animations with reduced motion
查看>>
[ARIA] What is Accessible Name Calculation?
查看>>