Top Description Fields Constructors Methods
org.springframework.web.socket.server.support

public Class HttpSessionHandshakeInterceptor

extends Object
implements HandshakeInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.socket.server.HandshakeInterceptor
Imports
java.util.Collection, .Collections, .Enumeration, .Map, jakarta.servlet.http.HttpSession, org.springframework.http.server.ServerHttpRequest, .ServerHttpResponse, .ServletServerHttpRequest, org.springframework.lang.Nullable, org.springframework.web.socket.WebSocketHandler, .WebSocketSession, org.springframework.web.socket.server.HandshakeInterceptor

An interceptor to copy information from the HTTP session to the "handshake attributes" map to be made available via WebSocketSession#getAttributes().

Copies a subset or all HTTP session attributes and/or the HTTP session ID under the key HTTP_SESSION_ID_ATTR_NAME.

Author
Rossen Stoyanchev
Since
4.0

Field Summary

Modifier and TypeField and Description
private final Collection<String>
private boolean
private boolean
private boolean
public static final String
HTTP_SESSION_ID_ATTR_NAME

The name of the attribute under which the HTTP session id is exposed when copyHttpSessionId is "true".

Constructor Summary

AccessConstructor and Description
public
HttpSessionHandshakeInterceptor()

Default constructor for copying all HTTP session attributes and the HTTP session id.

public
HttpSessionHandshakeInterceptor(Collection<String>
session attributes to copy
attributeNames
)

Constructor for copying specific HTTP session attributes and the HTTP session id.

Method Summary

Modifier and TypeMethod and Description
public void
afterHandshake(ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
WebSocketHandler
the target WebSocket handler
wsHandler
,
Exception
an exception raised during the handshake, or null if none
ex
)

Implements org.springframework.web.socket.server.HandshakeInterceptor.afterHandshake.

Invoked after the handshake is done.

public boolean
beforeHandshake(ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
WebSocketHandler
the target WebSocket handler
wsHandler
,
Map<String, Object>
the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
attributes
)

Implements org.springframework.web.socket.server.HandshakeInterceptor.beforeHandshake.

Invoked before the handshake is processed.

public Collection<String>
getAttributeNames()

Return the configured attribute names to copy (read-only).

private HttpSession
public boolean
isCopyAllAttributes()

Whether to copy all HTTP session attributes.

public boolean
isCopyHttpSessionId()

Whether to copy the HTTP session id to the handshake attributes.

public boolean
isCreateSession()

Whether the HTTP session is allowed to be created.

public void
setCopyAllAttributes(boolean
whether to copy all attributes
copyAllAttributes
)

Whether to copy all attributes from the HTTP session.

public void
setCopyHttpSessionId(boolean
whether to copy the HTTP session id.
copyHttpSessionId
)

Whether the HTTP session id should be copied to the handshake attributes under the key HTTP_SESSION_ID_ATTR_NAME.

public void
setCreateSession(boolean createSession)

Whether to allow the HTTP session to be created while accessing it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributeNamesback to summary
private final Collection<String> attributeNames
copyAllAttributesback to summary
private boolean copyAllAttributes
copyHttpSessionIdback to summary
private boolean copyHttpSessionId
createSessionback to summary
private boolean createSession
HTTP_SESSION_ID_ATTR_NAMEback to summary
public static final String HTTP_SESSION_ID_ATTR_NAME

The name of the attribute under which the HTTP session id is exposed when copyHttpSessionId is "true".

Constructor Detail

HttpSessionHandshakeInterceptorback to summary
public HttpSessionHandshakeInterceptor()

Default constructor for copying all HTTP session attributes and the HTTP session id.

See Also
setCopyAllAttributes, setCopyHttpSessionId
HttpSessionHandshakeInterceptorback to summary
public HttpSessionHandshakeInterceptor(Collection<String> attributeNames)

Constructor for copying specific HTTP session attributes and the HTTP session id.

Parameters
attributeNames:Collection<String>

session attributes to copy

See Also
setCopyAllAttributes, setCopyHttpSessionId

Method Detail

afterHandshakeback to summary
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex)

Implements org.springframework.web.socket.server.HandshakeInterceptor.afterHandshake.

Doc from org.springframework.web.socket.server.HandshakeInterceptor.afterHandshake.

Invoked after the handshake is done. The response status and headers indicate the results of the handshake, i.e. whether it was successful or not.

Parameters
request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

wsHandler:WebSocketHandler

the target WebSocket handler

ex:Exception

an exception raised during the handshake, or null if none

Annotations
@Override
beforeHandshakeback to summary
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception

Implements org.springframework.web.socket.server.HandshakeInterceptor.beforeHandshake.

Doc from org.springframework.web.socket.server.HandshakeInterceptor.beforeHandshake.

Invoked before the handshake is processed.

Parameters
request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

wsHandler:WebSocketHandler

the target WebSocket handler

attributes:Map<String, Object>

the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.

Returns:boolean

whether to proceed with the handshake (true) or abort (false)

Annotations
@Override
getAttributeNamesback to summary
public Collection<String> getAttributeNames()

Return the configured attribute names to copy (read-only).

getSessionback to summary
private HttpSession getSession(ServerHttpRequest request)
Annotations
@Nullable
isCopyAllAttributesback to summary
public boolean isCopyAllAttributes()

Whether to copy all HTTP session attributes.

isCopyHttpSessionIdback to summary
public boolean isCopyHttpSessionId()

Whether to copy the HTTP session id to the handshake attributes.

isCreateSessionback to summary
public boolean isCreateSession()

Whether the HTTP session is allowed to be created.

setCopyAllAttributesback to summary
public void setCopyAllAttributes(boolean copyAllAttributes)

Whether to copy all attributes from the HTTP session. If set to "true", any explicitly configured attribute names are ignored.

By default this is set to either "true" or "false" depending on which constructor was used (default or with attribute names respectively).

Parameters
copyAllAttributes:boolean

whether to copy all attributes

setCopyHttpSessionIdback to summary
public void setCopyHttpSessionId(boolean copyHttpSessionId)

Whether the HTTP session id should be copied to the handshake attributes under the key HTTP_SESSION_ID_ATTR_NAME.

By default this is "true".

Parameters
copyHttpSessionId:boolean

whether to copy the HTTP session id.

setCreateSessionback to summary
public void setCreateSession(boolean createSession)

Whether to allow the HTTP session to be created while accessing it.

By default set to false.

See Also
jakarta.servlet.http.HttpServletRequest#getSession(boolean)