简体   繁体   English

处理STOMP SEND时,在Spring中获取Websocket会话数据

[英]Get Websocket session data in Spring when handing STOMP SEND

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 ) Spring Websocket教程告诉我,如果我想处理STOMP SEND命令,我将使用( 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 然而,我需要知道哪个Websocket会话正在发送这个,以便进行检查

if (sessionIsAllowedToDoThings(sessionData))   {...}

How I can therefore get Websocket Session data for this example? 因此,我如何获得此示例的Websocket Session数据?

Well, you can obtain websocket's session id (and other fields) by adding org.springframework.messaging.simp.stomp.StompHeaderAccessor parameter to your handle(String) method: handle(String, StompHeaderAccessor) . 好吧,您可以通过将org.springframework.messaging.simp.stomp.StompHeaderAccessor参数添加到handle(String)方法: handle(String, StompHeaderAccessor)来获取websocket的会话ID(和其他字段handle(String, StompHeaderAccessor)

If you want to access your real JSESSIONID attribute, you have to create an implementation of org.springframework.web.socket.server.HandshakeInterceptor like so (it is written in Kotlin): 如果要访问真正的JSESSIONID属性,则必须像这样创建org.springframework.web.socket.server.HandshakeInterceptor的实现(它是用Kotlin编写的):

class HttpHandshakeInterceptor : HandshakeInterceptor {

    companion object {
        const val ATTRIBUTE_SESSION_ID = "sessionId"
    }

    override fun beforeHandshake(request: ServerHttpRequest, response: ServerHttpResponse, wsHandler: WebSocketHandler, attributes: MutableMap<String, Any>): Boolean {
        attributes[ATTRIBUTE_SESSION_ID] = (request as ServletServerHttpRequest).servletRequest.session.id
        return true
    }

    override fun afterHandshake(request: ServerHttpRequest, response: ServerHttpResponse, wsHandler: WebSocketHandler, exception: Exception?) {}
}

and register it wihtin your org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer like so: 并将其注册到您的org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer如下所示:

override fun registerStompEndpoints(registry: StompEndpointRegistry) {
    registry.addEndpoint("/endpoint").addInterceptors(httpHandshakeInterceptor)
}

The main idea here is that you intercept the initial handshake and store real session id within the websocket attributes. 这里的主要思想是拦截初始握手并在websocket属性中存储真实会话ID。 The attributes are available through StompHeaderAccessor you passed to your handle(String, StompHeaderAccessor) method. 这些属性可以通过StompHeaderAccessor传递给您的handle(String, StompHeaderAccessor)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM