Top Description Methods
org.springframework.web.server

public Interface WebSession

Known Direct Implementers
org.springframework.web.server.session.InMemoryWebSessionStore.InMemoryWebSession
Imports
java.time.Duration, .Instant, java.util.Map, reactor.core.publisher.Mono, org.springframework.lang.Nullable, org.springframework.util.Assert

Main contract for using a server-side session that provides access to session attributes across HTTP requests.

The creation of a WebSession instance does not automatically start a session thus causing the session id to be sent to the client (typically via a cookie). A session starts implicitly when session attributes are added. A session may also be created explicitly via start().

Author
Rossen Stoyanchev
Since
5.0

Method Summary

Modifier and TypeMethod and Description
public Mono<Void>

Returns:

completion notification (success or error)
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.

public default <
the attribute type
T
>
T

Returns:

the attribute value
getAttribute
(String
the attribute name
name
)

Return the session attribute value if present.

public default <
the attribute type
T
>
T

Returns:

the attribute value
getAttributeOrDefault
(String
the attribute name
name
,
T
a default value to return instead
defaultValue
)

Return the session attribute value, or a default, fallback value.

public Map<String, Object>
getAttributes()

Return a map that holds session attributes.

public Instant
getCreationTime()

Return the time when the session was created.

public String
getId()

Return a unique session identifier.

public Instant
getLastAccessTime()

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

public Duration
getMaxIdleTime()

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

public default <
the attribute type
T
>
T

Returns:

the attribute value
getRequiredAttribute
(String
the attribute name
name
)

Return the session attribute value or if not present raise an IllegalArgumentException.

public Mono<Void>

Returns:

completion notification (success or error)
invalidate
()

Invalidate the current session and clear session storage.

public boolean
isExpired()

Return true if the session expired after maxIdleTime elapsed.

public boolean
isStarted()

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

public Mono<Void>

Returns:

Mono to indicate completion with success or error
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)

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

public void
start()

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

Method Detail

changeSessionIdback to summary
public Mono<Void> 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)

getAttributeback to summary
public default <T> T getAttribute(String name)

Return the session attribute value if present.

Parameters
<T>
the attribute type
name:String

the attribute name

Returns:T

the attribute value

Annotations
@SuppressWarnings:unchecked
@Nullable
getAttributeOrDefaultback to summary
public default <T> T getAttributeOrDefault(String name, T defaultValue)

Return the session attribute value, or a default, fallback value.

Parameters
<T>
the attribute type
name:String

the attribute name

defaultValue:T

a default value to return instead

Returns:T

the attribute value

Annotations
@SuppressWarnings:unchecked
getAttributesback to summary
public Map<String, Object> getAttributes()

Return a map that holds session attributes.

getCreationTimeback to summary
public Instant getCreationTime()

Return the time when the session was created.

getIdback to summary
public String getId()

Return a unique session identifier.

getLastAccessTimeback to summary
public Instant 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.

getMaxIdleTimeback to summary
public Duration getMaxIdleTime()

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

getRequiredAttributeback to summary
public default <T> T getRequiredAttribute(String name)

Return the session attribute value or if not present raise an IllegalArgumentException.

Parameters
<T>
the attribute type
name:String

the attribute name

Returns:T

the attribute value

Annotations
@SuppressWarnings:unchecked
invalidateback to summary
public Mono<Void> invalidate()

Invalidate the current session and clear session storage.

Returns:Mono<Void>

completion notification (success or error)

isExpiredback to summary
public boolean 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.

isStartedback to summary
public boolean 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.

saveback to summary
public Mono<Void> 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

setMaxIdleTimeback to summary
public void setMaxIdleTime(Duration maxIdleTime)

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.

startback to summary
public void start()

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