|
|
|
@ -41,6 +41,7 @@ import org.springframework.web.server.WebSession; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rob Winch |
|
|
|
* @author Rob Winch |
|
|
|
|
|
|
|
* @author Mengqi Xu |
|
|
|
* @since 5.0 |
|
|
|
* @since 5.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class InMemoryWebSessionStore implements WebSessionStore { |
|
|
|
public class InMemoryWebSessionStore implements WebSessionStore { |
|
|
|
@ -50,6 +51,8 @@ public class InMemoryWebSessionStore implements WebSessionStore { |
|
|
|
|
|
|
|
|
|
|
|
private int maxSessions = 10000; |
|
|
|
private int maxSessions = 10000; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Duration defaultMaxIdleTime = Duration.ofMinutes(30); |
|
|
|
|
|
|
|
|
|
|
|
private Clock clock = Clock.systemUTC(); |
|
|
|
private Clock clock = Clock.systemUTC(); |
|
|
|
|
|
|
|
|
|
|
|
private final Map<String, InMemoryWebSession> sessions = new ConcurrentHashMap<>(); |
|
|
|
private final Map<String, InMemoryWebSession> sessions = new ConcurrentHashMap<>(); |
|
|
|
@ -77,6 +80,23 @@ public class InMemoryWebSessionStore implements WebSessionStore { |
|
|
|
return this.maxSessions; |
|
|
|
return this.maxSessions; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set the default maximum idle time for sessions. |
|
|
|
|
|
|
|
* <p>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 |
|
|
|
* Configure the {@link Clock} to use to set the {@code lastAccessTime} on |
|
|
|
* every created session and to calculate if the session has expired. |
|
|
|
* every created session and to calculate if the session has expired. |
|
|
|
@ -118,7 +138,7 @@ public class InMemoryWebSessionStore implements WebSessionStore { |
|
|
|
Instant now = this.clock.instant(); |
|
|
|
Instant now = this.clock.instant(); |
|
|
|
this.expiredSessionChecker.checkIfNecessary(now); |
|
|
|
this.expiredSessionChecker.checkIfNecessary(now); |
|
|
|
|
|
|
|
|
|
|
|
return Mono.<WebSession>fromSupplier(() -> new InMemoryWebSession(now)) |
|
|
|
return Mono.<WebSession>fromSupplier(() -> new InMemoryWebSession(now, this.defaultMaxIdleTime)) |
|
|
|
.subscribeOn(Schedulers.boundedElastic()) |
|
|
|
.subscribeOn(Schedulers.boundedElastic()) |
|
|
|
.publishOn(Schedulers.parallel()); |
|
|
|
.publishOn(Schedulers.parallel()); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -179,14 +199,15 @@ public class InMemoryWebSessionStore implements WebSessionStore { |
|
|
|
|
|
|
|
|
|
|
|
private volatile Instant lastAccessTime; |
|
|
|
private volatile Instant lastAccessTime; |
|
|
|
|
|
|
|
|
|
|
|
private volatile Duration maxIdleTime = Duration.ofMinutes(30); |
|
|
|
private volatile Duration maxIdleTime; |
|
|
|
|
|
|
|
|
|
|
|
private final AtomicReference<State> state = new AtomicReference<>(State.NEW); |
|
|
|
private final AtomicReference<State> state = new AtomicReference<>(State.NEW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public InMemoryWebSession(Instant creationTime) { |
|
|
|
public InMemoryWebSession(Instant creationTime, Duration maxIdleTime) { |
|
|
|
this.creationTime = creationTime; |
|
|
|
this.creationTime = creationTime; |
|
|
|
this.lastAccessTime = this.creationTime; |
|
|
|
this.lastAccessTime = this.creationTime; |
|
|
|
|
|
|
|
this.maxIdleTime = maxIdleTime; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|