在 WebSocketHandler (Spring-websocket) 中访问 HTTP Session
Access HTTP Session in WebSocketHandler (Spring-websocket)
亲爱的,
我正在尝试在我的 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 {
}
然后我注入了修改握手的 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 会话中需要什么,都将在 WebSocketSession.getAttributes()
中可用。
亲爱的, 我正在尝试在我的 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 {
}
然后我注入了修改握手的 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 会话中需要什么,都将在 WebSocketSession.getAttributes()
中可用。