java - 在WebSocketHandler中访问HTTP session (Spring-websocket)

标签 java websocket spring-websocket httpsession

亲爱的, 我正在尝试在我的 WebSocketHandler 中获取 HTTPSession。当我使用“javax.websocket-api”时,我可以成功完成此操作,但我现在使用“Spring-Websocket”。

配置:

@ConditionalOnWebApplication
@Configuration
@EnableWebSocket
public class WebSocketConfigurator implements WebSocketConfigurer {

    @Autowired
    private ApplicationContext context;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        MyEndpoint endpoint = context.getBean(MyEndpoint.class);
        registry.addHandler(endpoint, "/signaling");
    }
}

建立连接时:

    @Component
    public class MyEndpoint implements WebSocketHandler {

        private WebSocketSession wsSession;

        @Override
        public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
            this.wsSession = webSocketSession;
            // need to get the HTTP SESSION HERE        
            log.info("Opening: " + webSocketSession.getId());
        }
    }

现在这是我如何使用“javax.websocket-api”来完成此操作的示例:

配置:

@ServerEndpoint(value = "/signaling", //
        decoders = MessageDecoder.class, //
        encoders = MessageEncoder.class,
        configurator = MyEndpointConfigurator.class)
/***
 * define signaling endpoint         
 */
public class MyEndpoint extends NextRTCEndpoint {

}

然后我注入(inject) HTTPSession 修改握手:

public class MyEndpointConfigurator extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig config,
            HandshakeRequest request,
            HandshakeResponse response) {
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        config.getUserProperties().put(HttpSession.class.getName(), httpSession);
    }
}

最后,当 WS 连接建立时就可以访问了:

    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.wsSession = session;
        this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        log.info("Opening: " + session.getId());

        server.register(session, httpSession);
    }

我无法成功地用“Spring Websocket”做类似的事情。有什么解决办法吗?请不要建议使用 StompJS 的类,因为我没有使用它。

最佳答案

有一个可以使用:

**
 * An interceptor to copy information from the HTTP session to the "handshake
 * attributes" map to made available via{@link WebSocketSession#getAttributes()}.
 *
 * <p>Copies a subset or all HTTP session attributes and/or the HTTP session id
 * under the key {@link #HTTP_SESSION_ID_ATTR_NAME}.
 *
 * @author Rossen Stoyanchev
 * @since 4.0
 */
public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor {

Reference Manual中有一个样本如何配置:

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(new MyHandler(), "/myHandler")
        .addInterceptors(new HttpSessionHandshakeInterceptor());
}

因此,您从 HTTP session 中需要的任何内容都可以在 WebSocketSession.getAttributes() 中获得。

关于java - 在WebSocketHandler中访问HTTP session (Spring-websocket),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51757269/

相关文章:

node.js - Angular 通用服务器渲染 WebSocket

PHP Web 套接字无法在 SSL 中连接

go - 无法将 websocket 数据从 flutter 应用程序发送到服务器

java - 更改 websocket 范围(从应用程序到 session / View )

spring-security - 通过 SockJS 通过 STOMP 进行安全 WebSocket 升级失败,升级 header 无效

java - 在 Java 中使用字符串缓冲区进行字符串追加

java - Craftercms : How to access Crafter engine properties in groovy controllers?

spring - 如何为spring sockjs websocket服务器实现增加输出缓冲区

java - 将 java 对象逆向工程为 xsd

java - Ant Ivy 不会设置编译类路径