Spring Websocket tutorial tells that if I like to handle STOMP SEND command, I shall use (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html)
@Controller
public class GreetingController {
@MessageMapping("/greeting") {
public String handle(String greeting) {
return "[" + getTimestamp() + ": " + greeting;
}
}
I need however also know which Websocket Session was sending this, in order to do the check like
if (sessionIsAllowedToDoThings(sessionData)) {...}
How I can therefore get Websocket Session data for this example?
Well, you can obtain websocket's session id (and other fields) by adding
org.springframework.messaging.simp.stomp.StompHeaderAccessorparameter to yourhandle(String)method:handle(String, StompHeaderAccessor).If you want to access your real
JSESSIONIDattribute, you have to create an implementation oforg.springframework.web.socket.server.HandshakeInterceptorlike so (it is written in Kotlin):and register it wihtin your
org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurerlike so:The main idea here is that you intercept the initial handshake and store real session id within the websocket attributes. The attributes are available through
StompHeaderAccessoryou passed to yourhandle(String, StompHeaderAccessor)method.