- Type Parameters:
A
- the session data type
- All Known Subinterfaces:
JWTSession<A,
B>
- All Known Implementing Classes:
AbstractJWTSession
,AbstractSession
A session provides a way to persist data across multiple requests.
A session is set to expire after a specific period of inactivity or at a specific time. The maximum inactive interval or the expiration time are specified on session creation and can be updated during the lifetime of the session. A session lasts as long as it is not expired and not explicitly invalidated.
A session is uniquely identify by an id generated by a SessionIdGenerator
. This id is used to resolve the session between requests from a SessionStore
. It can be refreshed during
the lifetime of the session.
A session may hold strongly typed data stored in the SessionStore
, but this is not mandatory. A session can exist and can be resolved independently of the data it may or may not contain.
- Since:
- 1.13
- Author:
- Jeremy Kuhn
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
The default maximum inactive interval in milliseconds:1800000
(i.e. 30 minutes). -
Method Summary
Modifier and TypeMethodDescriptionlong
Returns the session creation time in milliseconds.getData()
Resolves the session data.Resolves the session data or creates them using the specified supplier if none exist in the session.long
Returns the time in milliseconds at which the session is set to expire.getId()
Returns the session unique identifier.long
Returns the session last accessed time in milliseconds.Returns the maximum inactive interval in milliseconds.Returns the original session identifier.Invalidates the session.boolean
Determines whether the session has expired.boolean
Determines whether the session has been invalidated.default boolean
isNew()
Determines whether the session is new.Refreshes the session id only if it is stale.refreshId
(boolean force) Refreshes the session id.save()
Saves the session.void
Sets the session data.void
setExpirationTime
(long expirationTime) Sets the session expiration time in milliseconds.void
setMaxInactiveInterval
(long maxInactiveInterval) Sets the maximum inactive interval in milliseconds.
-
Field Details
-
DEFAULT_MAX_INACTIVE_INTERVAL
static final long DEFAULT_MAX_INACTIVE_INTERVALThe default maximum inactive interval in milliseconds:1800000
(i.e. 30 minutes).- See Also:
-
-
Method Details
-
getOriginalId
String getOriginalId()Returns the original session identifier.
The original session id is the value that was used to resolve an existing session or
null
for a new session.- Returns:
- the original session id for an existing session or
null
if the session is new
-
getId
String getId()Returns the session unique identifier.
- Returns:
- a unique identifier
-
getCreationTime
long getCreationTime()Returns the session creation time in milliseconds.
- Returns:
- the session creation time
-
getLastAccessedTime
long getLastAccessedTime()Returns the session last accessed time in milliseconds.
The last accessed time shall only be updated when saving a session, it is not updated when the session is resolved and as a result the last accessed time effectively corresponds to the last saved time.
- Returns:
- the session last accessed time
-
setMaxInactiveInterval
void setMaxInactiveInterval(long maxInactiveInterval) Sets the maximum inactive interval in milliseconds.
The maximum inactive interval replaces the expiration time if any was set.
- Parameters:
maxInactiveInterval
- a maximum inactive interval in milliseconds
-
getMaxInactiveInterval
Long getMaxInactiveInterval()Returns the maximum inactive interval in milliseconds.
The maximum inactive interval is
null
when a specific expiration time was set.- Returns:
- the maximum inactive interval in milliseconds or
null
if the session is set to expire at a specific time
-
setExpirationTime
void setExpirationTime(long expirationTime) Sets the session expiration time in milliseconds.
The expiration time replaces the maximum inactive interval if any was set.
- Parameters:
expirationTime
- an expiration time in milliseconds
-
getExpirationTime
long getExpirationTime()Returns the time in milliseconds at which the session is set to expire.
This method calculates and returns the actual time when the session will expire when a maximum inactive interval is set, otherwise it returns the specific expiration time that was set.
- Returns:
- the session expiration time
-
getData
Resolves the session data.
- Returns:
- a mono resolving session data or an empty mono if none exist in the session
-
getData
Resolves the session data or creates them using the specified supplier if none exist in the session.
- Parameters:
supplier
- a session data supplier- Returns:
- a mono resolving or creating session data
-
setData
Sets the session data.
- Parameters:
sessionData
- the session data
-
isExpired
boolean isExpired()Determines whether the session has expired.
- Returns:
- true if the session has expired, false otherwise
-
isInvalidated
boolean isInvalidated()Determines whether the session has been invalidated.
- Returns:
- true if the session has been invalidated, false otherwise
-
isNew
default boolean isNew()Determines whether the session is new.
- Returns:
- true if this is a new session, false otherwise
-
refreshId
Refreshes the session id only if it is stale.
- Returns:
- a mono that emits the new session identifier or an empty mono if the identifier did not need to be refreshed
-
refreshId
Refreshes the session id.
- Parameters:
force
- true to force refresh, false to only refresh the id if it is stale- Returns:
- a mono that emits the new session identifier or an empty mono if the identifier did not need to be refreshed
-
save
Saves the session.
This basically updates the session last accessed time and any updates to expiration settings.
Assuming session data were resolved, whether they are saved is implementation specific. This actually widely depends on the session store and the strategy chosen to optimize underlying data store usage. Implementors must however at least guarantee that data that are explicitly set on a session using
setData(Object)
are saved.- Returns:
- a mono which completes when the session is saved
-
invalidate
Invalidates the session.
- Returns:
- a mono which completes when the session is invalidated
-