Package org.springframework.session
Interface Session
- All Known Implementing Classes:
MapSession
public interface Session
Provides a way to identify a user in an agnostic way. This allows the session to be
used by an HttpSession, WebSocket Session, or even non web related sessions.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionChanges the session id.<T> T
getAttribute
(String attributeName) Gets the Object associated with the specified name or null if no Object is associated to that name.Gets the attribute names that have a value associated with it.default <T> T
getAttributeOrDefault
(String name, T defaultValue) Return the session attribute value, or a default, fallback value.Gets the time when this session was created.getId()
Gets a unique string that identifies theSession
.Gets the last time thisSession
was accessed.Gets the maximum inactive interval between requests before this session will be invalidated.default <T> T
getRequiredAttribute
(String name) Return the session attribute value or if not present raise anIllegalArgumentException
.boolean
Returns true if the session is expired.void
removeAttribute
(String attributeName) Removes the attribute with the provided attribute name.void
setAttribute
(String attributeName, Object attributeValue) Sets the attribute value for the provided attribute name.void
setLastAccessedTime
(Instant lastAccessedTime) Sets the last accessed time.void
setMaxInactiveInterval
(Duration interval) Sets the maximum inactive interval between requests before this session will be invalidated.
-
Method Details
-
getId
String getId()Gets a unique string that identifies theSession
.- Returns:
- a unique string that identifies the
Session
-
changeSessionId
String changeSessionId()Changes the session id. After invoking thegetId()
will return a new identifier.- Returns:
- the new session id which
getId()
will now return
-
getAttribute
Gets the Object associated with the specified name or null if no Object is associated to that name.- Type Parameters:
T
- the return type of the attribute- Parameters:
attributeName
- the name of the attribute to get- Returns:
- the Object associated with the specified name or null if no Object is associated to that name
-
getRequiredAttribute
Return the session attribute value or if not present raise anIllegalArgumentException
.- Type Parameters:
T
- the attribute type- Parameters:
name
- the attribute name- Returns:
- the attribute value
-
getAttributeOrDefault
Return the session attribute value, or a default, fallback value.- Type Parameters:
T
- the attribute type- Parameters:
name
- the attribute namedefaultValue
- a default value to return instead- Returns:
- the attribute value
-
getAttributeNames
Gets the attribute names that have a value associated with it. Each value can be passed intogetAttribute(String)
to obtain the attribute value.- Returns:
- the attribute names that have a value associated with it.
- See Also:
-
setAttribute
Sets the attribute value for the provided attribute name. If the attributeValue is null, it has the same result as removing the attribute withremoveAttribute(String)
.- Parameters:
attributeName
- the attribute name to setattributeValue
- the value of the attribute to set. If null, the attribute will be removed.
-
removeAttribute
Removes the attribute with the provided attribute name.- Parameters:
attributeName
- the name of the attribute to remove
-
getCreationTime
Instant getCreationTime()Gets the time when this session was created.- Returns:
- the time when this session was created.
-
setLastAccessedTime
Sets the last accessed time.- Parameters:
lastAccessedTime
- the last accessed time
-
getLastAccessedTime
Instant getLastAccessedTime()Gets the last time thisSession
was accessed.- Returns:
- the last time the client sent a request associated with the session
-
setMaxInactiveInterval
Sets the maximum inactive interval between requests before this session will be invalidated. A negative time indicates that the session will never timeout.- Parameters:
interval
- the amount of time that theSession
should be kept alive between client requests.
-
getMaxInactiveInterval
Duration getMaxInactiveInterval()Gets the maximum inactive interval between requests before this session will be invalidated. A negative time indicates that the session will never timeout.- Returns:
- the maximum inactive interval between requests before this session will be invalidated. A negative time indicates that the session will never timeout.
-
isExpired
boolean isExpired()Returns true if the session is expired.- Returns:
- true if the session is expired, else false.
-