Class SessionRepositoryMessageInterceptor<S extends Session>
java.lang.Object
org.springframework.session.web.socket.server.SessionRepositoryMessageInterceptor<S>
- Type Parameters:
S
- theSession
type
- All Implemented Interfaces:
org.springframework.messaging.support.ChannelInterceptor
,org.springframework.web.socket.server.HandshakeInterceptor
public final class SessionRepositoryMessageInterceptor<S extends Session>
extends Object
implements org.springframework.messaging.support.ChannelInterceptor, org.springframework.web.socket.server.HandshakeInterceptor
Acts as a ChannelInterceptor
and a HandshakeInterceptor
to ensure the
Session.getLastAccessedTime()
is up to date.
- Associates the
Session.getId()
with the WebSocket Session attributes when the handshake is performed. This is later used when intercepting messages to ensure theSession.getLastAccessedTime()
is updated. - Intercepts
Message
's that are haveSimpMessageType
that corresponds tosetMatchingMessageTypes(Set)
and updates the last accessed time of theSession
. If theSession
is expired, theMessage
is prevented from proceeding.
In order to work SessionRepositoryMessageInterceptor
must be registered as a
ChannelInterceptor
and a HandshakeInterceptor
.
- Since:
- 1.0
-
Constructor Summary
ConstructorDescriptionSessionRepositoryMessageInterceptor
(SessionRepository<S> sessionRepository) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterHandshake
(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, org.springframework.web.socket.WebSocketHandler wsHandler, Exception exception) boolean
beforeHandshake
(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, org.springframework.web.socket.WebSocketHandler wsHandler, Map<String, Object> attributes) static String
getSessionId
(Map<String, Object> attributes) org.springframework.messaging.Message<?>
preSend
(org.springframework.messaging.Message<?> message, org.springframework.messaging.MessageChannel channel) void
setMatchingMessageTypes
(Set<org.springframework.messaging.simp.SimpMessageType> matchingMessageTypes) Sets theSimpMessageType
to match on.static void
setSessionId
(Map<String, Object> attributes, String sessionId) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.messaging.support.ChannelInterceptor
afterReceiveCompletion, afterSendCompletion, postReceive, postSend, preReceive
-
Constructor Details
-
SessionRepositoryMessageInterceptor
Creates a new instance.- Parameters:
sessionRepository
- theSessionRepository
to use. Cannot be null.
-
-
Method Details
-
setMatchingMessageTypes
public void setMatchingMessageTypes(Set<org.springframework.messaging.simp.SimpMessageType> matchingMessageTypes) Sets the
SimpMessageType
to match on. If theMessage
matches, thenpreSend(Message, MessageChannel)
ensures theSession
is not expired and updates theSession.getLastAccessedTime()
The default is: SimpMessageType.CONNECT, SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE, SimpMessageType.UNSUBSCRIBE.
- Parameters:
matchingMessageTypes
- theSimpMessageType
to match on inpreSend(Message, MessageChannel)
, else theMessage
is continued without accessing or updating theSession
-
preSend
public org.springframework.messaging.Message<?> preSend(org.springframework.messaging.Message<?> message, org.springframework.messaging.MessageChannel channel) - Specified by:
preSend
in interfaceorg.springframework.messaging.support.ChannelInterceptor
-
beforeHandshake
public boolean beforeHandshake(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, org.springframework.web.socket.WebSocketHandler wsHandler, Map<String, Object> attributes) - Specified by:
beforeHandshake
in interfaceorg.springframework.web.socket.server.HandshakeInterceptor
-
afterHandshake
public void afterHandshake(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, org.springframework.web.socket.WebSocketHandler wsHandler, Exception exception) - Specified by:
afterHandshake
in interfaceorg.springframework.web.socket.server.HandshakeInterceptor
-
getSessionId
-
setSessionId
-