diff --git a/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java b/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java index 58c949bfaf38..b568d320d05f 100644 --- a/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java +++ b/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java @@ -42,6 +42,7 @@ * * @author Rossen Stoyanchev * @author Rob Winch + * @author Mengqi Xu * @since 5.0 */ public class InMemoryWebSessionStore implements WebSessionStore { @@ -51,6 +52,8 @@ public class InMemoryWebSessionStore implements WebSessionStore { private int maxSessions = 10000; + private Duration defaultMaxIdleTime = Duration.ofMinutes(30); + private Clock clock = Clock.system(ZoneId.of("GMT")); private final Map sessions = new ConcurrentHashMap<>(); @@ -78,6 +81,23 @@ public int getMaxSessions() { return this.maxSessions; } + /** + * Set the default maximum idle time for sessions. + *

By default, set to 30 minutes. + * @param maxIdleTime the default max idle time + */ + public void setDefaultMaxIdleTime(Duration maxIdleTime) { + Assert.notNull(maxIdleTime, "maxIdleTime is required"); + this.defaultMaxIdleTime = maxIdleTime; + } + + /** + * Return the default maximum idle time for sessions. + */ + public Duration getDefaultMaxIdleTime() { + return this.defaultMaxIdleTime; + } + /** * Configure the {@link Clock} to use to set the {@code lastAccessTime} on * every created session and to calculate if the session has expired. @@ -119,7 +139,7 @@ public Mono createWebSession() { Instant now = this.clock.instant(); this.expiredSessionChecker.checkIfNecessary(now); - return Mono.fromSupplier(() -> new InMemoryWebSession(now)) + return Mono.fromSupplier(() -> new InMemoryWebSession(now, this.defaultMaxIdleTime)) .subscribeOn(Schedulers.boundedElastic()) .publishOn(Schedulers.parallel()); } @@ -180,14 +200,15 @@ private class InMemoryWebSession implements WebSession { private volatile Instant lastAccessTime; - private volatile Duration maxIdleTime = Duration.ofMinutes(30); + private volatile Duration maxIdleTime; private final AtomicReference state = new AtomicReference<>(State.NEW); - public InMemoryWebSession(Instant creationTime) { + public InMemoryWebSession(Instant creationTime, Duration maxIdleTime) { this.creationTime = creationTime; this.lastAccessTime = this.creationTime; + this.maxIdleTime = maxIdleTime; } @Override