Top Description Inners Fields Constructors Methods
org.springframework.web.server.session

public Class InMemoryWebSessionStore

extends Object
implements WebSessionStore
Class Inheritance
All Implemented Interfaces
org.springframework.web.server.session.WebSessionStore
Imports
java.time.Clock, .Duration, .Instant, .ZoneId, java.time.temporal.ChronoUnit, java.util.Collections, .Iterator, .Map, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicReference, java.util.concurrent.locks.Lock, .ReentrantLock, reactor.core.publisher.Mono, reactor.core.scheduler.Schedulers, org.springframework.util.Assert, .IdGenerator, .JdkIdGenerator, org.springframework.web.server.WebSession

Simple Map-based storage for WebSession instances.
Authors
Rossen Stoyanchev, Rob Winch
Since
5.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class
private static enum

Field Summary

Modifier and TypeField and Description
private Clock
private final InMemoryWebSessionStore.ExpiredSessionChecker
private static final IdGenerator
private int
private final Map<String, InMemoryWebSessionStore.InMemoryWebSession>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Mono<WebSession>
public Clock
getClock()

Return the configured clock for session lastAccessTime calculations.

public int
getMaxSessions()

Return the maximum number of sessions that can be stored.

public Map<String, WebSession>
getSessions()

Return the map of sessions with an unmodifiable wrapper.

public void
removeExpiredSessions()

Check for expired sessions and remove them.

public Mono<Void>
removeSession(String
the id of the session to remove
id
)

Implements org.springframework.web.server.session.WebSessionStore.removeSession.

Remove the WebSession for the specified id.

public Mono<WebSession>
retrieveSession(String
the session to load
id
)

Implements org.springframework.web.server.session.WebSessionStore.retrieveSession.

Return the WebSession for the given id.

Note

This method should perform an expiration check, and if it has expired remove the session and return empty.

public void
setClock(Clock
the clock to use
clock
)

Configure the Clock to use to set lastAccessTime on every created session and to calculate if it is expired.

public void
setMaxSessions(int
the maximum number of sessions
maxSessions
)

Set the maximum number of sessions that can be stored.

public Mono<WebSession>
updateLastAccessTime(WebSession
the session to update
session
)

Implements org.springframework.web.server.session.WebSessionStore.updateLastAccessTime.

Update the last accessed timestamp to "now".

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clockback to summary
private Clock clock
expiredSessionCheckerback to summary
private final InMemoryWebSessionStore.ExpiredSessionChecker expiredSessionChecker
idGeneratorback to summary
private static final IdGenerator idGenerator
maxSessionsback to summary
private int maxSessions
sessionsback to summary
private final Map<String, InMemoryWebSessionStore.InMemoryWebSession> sessions

Constructor Detail

InMemoryWebSessionStoreback to summary
public InMemoryWebSessionStore()

Method Detail

createWebSessionback to summary
public Mono<WebSession> createWebSession()

Implements org.springframework.web.server.session.WebSessionStore.createWebSession.

Doc from org.springframework.web.server.session.WebSessionStore.createWebSession.

Create a new WebSession.

Note that this does nothing more than create a new instance. The session can later be started explicitly via WebSession#start() or implicitly by adding attributes -- and then persisted via WebSession#save().

Returns:Mono<WebSession>

the created session instance

Annotations
@Override
getClockback to summary
public Clock getClock()

Return the configured clock for session lastAccessTime calculations.

getMaxSessionsback to summary
public int getMaxSessions()

Return the maximum number of sessions that can be stored.

Since
5.0.8
getSessionsback to summary
public Map<String, WebSession> getSessions()

Return the map of sessions with an unmodifiable wrapper. This could be used for management purposes, to list active sessions, invalidate expired ones, etc.

Since
5.0.8
removeExpiredSessionsback to summary
public void removeExpiredSessions()

Check for expired sessions and remove them. Typically such checks are kicked off lazily during calls to create or retrieve, no less than 60 seconds apart. This method can be called to force a check at a specific time.

Since
5.0.8
removeSessionback to summary
public Mono<Void> removeSession(String id)

Implements org.springframework.web.server.session.WebSessionStore.removeSession.

Doc from org.springframework.web.server.session.WebSessionStore.removeSession.

Remove the WebSession for the specified id.

Parameters
id:String

the id of the session to remove

Returns:Mono<Void>

a completion notification (success or error)

Annotations
@Override
retrieveSessionback to summary
public Mono<WebSession> retrieveSession(String id)

Implements org.springframework.web.server.session.WebSessionStore.retrieveSession.

Doc from org.springframework.web.server.session.WebSessionStore.retrieveSession.

Return the WebSession for the given id.

Note

This method should perform an expiration check, and if it has expired remove the session and return empty. This method should also update the lastAccessTime of retrieved sessions.

Parameters
id:String

the session to load

Returns:Mono<WebSession>

the session, or an empty Mono

Annotations
@Override
setClockback to summary
public void setClock(Clock clock)

Configure the Clock to use to set lastAccessTime on every created session and to calculate if it is expired.

This may be useful to align to different timezone or to set the clock back in a test, e.g. Clock.offset(clock, Duration.ofMinutes(-31)) in order to simulate session expiration.

By default this is Clock.system(ZoneId.of("GMT")).

Parameters
clock:Clock

the clock to use

setMaxSessionsback to summary
public void setMaxSessions(int maxSessions)

Set the maximum number of sessions that can be stored. Once the limit is reached, any attempt to store an additional session will result in an IllegalStateException.

By default set to 10000.

Parameters
maxSessions:int

the maximum number of sessions

Since
5.0.8
updateLastAccessTimeback to summary
public Mono<WebSession> updateLastAccessTime(WebSession session)

Implements org.springframework.web.server.session.WebSessionStore.updateLastAccessTime.

Doc from org.springframework.web.server.session.WebSessionStore.updateLastAccessTime.

Update the last accessed timestamp to "now".

Parameters
session:WebSession

the session to update

Returns:Mono<WebSession>

the session with the updated last access time

Annotations
@Override
org.springframework.web.server.session back to summary

private Class InMemoryWebSessionStore.ExpiredSessionChecker

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.server.session.InMemoryWebSessionStore.ExpiredSessionChecker

Field Summary

Modifier and TypeField and Description
private static final int
CHECK_PERIOD

Max time between expiration checks.

private Instant
private final Lock

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHECK_PERIODback to summary
private static final int CHECK_PERIOD

Max time between expiration checks.

checkTimeback to summary
private Instant checkTime
lockback to summary
private final Lock lock

Constructor Detail

ExpiredSessionCheckerback to summary
private ExpiredSessionChecker()

Method Detail

checkIfNecessaryback to summary
public void checkIfNecessary(Instant now)
removeExpiredSessionsback to summary
public void removeExpiredSessions(Instant now)
org.springframework.web.server.session back to summary

private Class InMemoryWebSessionStore.InMemoryWebSession

extends Object
implements WebSession
Class Inheritance
  • java.lang.Object
  • org.springframework.web.server.session.InMemoryWebSessionStore.InMemoryWebSession
All Implemented Interfaces
org.springframework.web.server.WebSession

Field Summary

Modifier and TypeField and Description
private final Map<String, Object>
private final Instant
private final AtomicReference<String>
private volatile Instant
private volatile Duration
private final AtomicReference<InMemoryWebSessionStore.State>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Mono<Void>
changeSessionId()

Implements org.springframework.web.server.WebSession.changeSessionId.

Generate a new id for the session and update the underlying session storage to reflect the new id. After a successful call getId() reflects the new session id.

private boolean
checkExpired(Instant currentTime)

private void
public Map<String, Object>
getAttributes()

Implements org.springframework.web.server.WebSession.getAttributes.

Return a map that holds session attributes.

public Instant
getCreationTime()

Implements org.springframework.web.server.WebSession.getCreationTime.

Return the time when the session was created.

public String
getId()

Implements org.springframework.web.server.WebSession.getId.

Return a unique session identifier.

public Instant
getLastAccessTime()

Implements org.springframework.web.server.WebSession.getLastAccessTime.

Return the last time of session access as a result of user activity such as an HTTP request.

public Duration
getMaxIdleTime()

Implements org.springframework.web.server.WebSession.getMaxIdleTime.

Return the maximum time after the lastAccessTime before a session expires.

public Mono<Void>
invalidate()

Implements org.springframework.web.server.WebSession.invalidate.

Invalidate the current session and clear session storage.

public boolean
isExpired()

Implements org.springframework.web.server.WebSession.isExpired.

Return true if the session expired after maxIdleTime elapsed.

private boolean
public boolean
isStarted()

Implements org.springframework.web.server.WebSession.isStarted.

Whether a session with the client has been started explicitly via start() or implicitly by adding session attributes.

public Mono<Void>
save()

Implements org.springframework.web.server.WebSession.save.

Save the session through the WebSessionStore as follows:

  • If the session is new (i.e. created but never persisted), it must have been started explicitly via start() or implicitly by adding attributes, or otherwise this method should have no effect.
  • If the session was retrieved through the WebSessionStore, the implementation for this method must check whether the session was invalidated and if so return an error.
public void
setMaxIdleTime(Duration maxIdleTime)

Implements org.springframework.web.server.WebSession.setMaxIdleTime.

Configure the max amount of time that may elapse after the lastAccessTime before a session is considered expired.

public void
start()

Implements org.springframework.web.server.WebSession.start.

Force the creation of a session causing the session id to be sent when save() is called.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributesback to summary
private final Map<String, Object> attributes
creationTimeback to summary
private final Instant creationTime
idback to summary
private final AtomicReference<String> id
lastAccessTimeback to summary
private volatile Instant lastAccessTime
maxIdleTimeback to summary
private volatile Duration maxIdleTime
stateback to summary
private final AtomicReference<InMemoryWebSessionStore.State> state

Constructor Detail

InMemoryWebSessionback to summary
public InMemoryWebSession(Instant creationTime)

Method Detail

changeSessionIdback to summary
public Mono<Void> changeSessionId()

Implements org.springframework.web.server.WebSession.changeSessionId.

Doc from org.springframework.web.server.WebSession.changeSessionId.

Generate a new id for the session and update the underlying session storage to reflect the new id. After a successful call getId() reflects the new session id.

Returns:Mono<Void>

completion notification (success or error)

Annotations
@Override
checkExpiredback to summary
private boolean checkExpired(Instant currentTime)
checkMaxSessionsLimitback to summary
private void checkMaxSessionsLimit()
getAttributesback to summary
public Map<String, Object> getAttributes()

Implements org.springframework.web.server.WebSession.getAttributes.

Doc from org.springframework.web.server.WebSession.getAttributes.

Return a map that holds session attributes.

Annotations
@Override
getCreationTimeback to summary
public Instant getCreationTime()

Implements org.springframework.web.server.WebSession.getCreationTime.

Doc from org.springframework.web.server.WebSession.getCreationTime.

Return the time when the session was created.

Annotations
@Override
getIdback to summary
public String getId()

Implements org.springframework.web.server.WebSession.getId.

Doc from org.springframework.web.server.WebSession.getId.

Return a unique session identifier.

Annotations
@Override
getLastAccessTimeback to summary
public Instant getLastAccessTime()

Implements org.springframework.web.server.WebSession.getLastAccessTime.

Doc from org.springframework.web.server.WebSession.getLastAccessTime.

Return the last time of session access as a result of user activity such as an HTTP request. Together with maxIdleTimeInSeconds this helps to determine when a session is expired.

Annotations
@Override
getMaxIdleTimeback to summary
public Duration getMaxIdleTime()

Implements org.springframework.web.server.WebSession.getMaxIdleTime.

Doc from org.springframework.web.server.WebSession.getMaxIdleTime.

Return the maximum time after the lastAccessTime before a session expires. A negative time indicates the session doesn't expire.

Annotations
@Override
invalidateback to summary
public Mono<Void> invalidate()

Implements org.springframework.web.server.WebSession.invalidate.

Doc from org.springframework.web.server.WebSession.invalidate.

Invalidate the current session and clear session storage.

Returns:Mono<Void>

completion notification (success or error)

Annotations
@Override
isExpiredback to summary
public boolean isExpired()

Implements org.springframework.web.server.WebSession.isExpired.

Doc from org.springframework.web.server.WebSession.isExpired.

Return true if the session expired after maxIdleTime elapsed.

Typically expiration checks should be automatically made when a session is accessed, a new WebSession instance created if necessary, at the start of request processing so that applications don't have to worry about expired session by default.

Annotations
@Override
isExpiredback to summary
private boolean isExpired(Instant now)
isStartedback to summary
public boolean isStarted()

Implements org.springframework.web.server.WebSession.isStarted.

Doc from org.springframework.web.server.WebSession.isStarted.

Whether a session with the client has been started explicitly via start() or implicitly by adding session attributes. If "false" then the session id is not sent to the client and the save() method is essentially a no-op.

Annotations
@Override
saveback to summary
public Mono<Void> save()

Implements org.springframework.web.server.WebSession.save.

Doc from org.springframework.web.server.WebSession.save.

Save the session through the WebSessionStore as follows:

  • If the session is new (i.e. created but never persisted), it must have been started explicitly via start() or implicitly by adding attributes, or otherwise this method should have no effect.
  • If the session was retrieved through the WebSessionStore, the implementation for this method must check whether the session was invalidated and if so return an error.

Note that this method is not intended for direct use by applications. Instead it is automatically invoked just before the response is committed.

Returns:Mono<Void>

Mono to indicate completion with success or error

Annotations
@Override
setMaxIdleTimeback to summary
public void setMaxIdleTime(Duration maxIdleTime)

Implements org.springframework.web.server.WebSession.setMaxIdleTime.

Doc from org.springframework.web.server.WebSession.setMaxIdleTime.

Configure the max amount of time that may elapse after the lastAccessTime before a session is considered expired. A negative value indicates the session should not expire.

Annotations
@Override
startback to summary
public void start()

Implements org.springframework.web.server.WebSession.start.

Doc from org.springframework.web.server.WebSession.start.

Force the creation of a session causing the session id to be sent when save() is called.

Annotations
@Override
updateLastAccessTimeback to summary
private void updateLastAccessTime(Instant currentTime)
org.springframework.web.server.session back to summary

private final Enum InMemoryWebSessionStore.State

extends Enum<InMemoryWebSessionStore.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final InMemoryWebSessionStore.State
public static final InMemoryWebSessionStore.State
public static final InMemoryWebSessionStore.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static InMemoryWebSessionStore.State
public static InMemoryWebSessionStore.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

EXPIREDback to summary
public static final InMemoryWebSessionStore.State EXPIRED
NEWback to summary
public static final InMemoryWebSessionStore.State NEW
STARTEDback to summary
public static final InMemoryWebSessionStore.State STARTED

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static InMemoryWebSessionStore.State valueOf(String name)
valuesback to summary
public static InMemoryWebSessionStore.State[] values()