Java 带websocket和会话拦截器的Spring+;Tomcat=问题
我面临着一个奇怪的问题。我正在用WebSocket开发基于Spring Boot的web应用程序。我需要跟踪匿名web套接字连接,因此我正在使用自定义Java 带websocket和会话拦截器的Spring+;Tomcat=问题,java,spring,websocket,Java,Spring,Websocket,我面临着一个奇怪的问题。我正在用WebSocket开发基于Spring Boot的web应用程序。我需要跟踪匿名web套接字连接,因此我正在使用自定义Principal实现,这是我在自定义DefaultHandshakeHandler->determinuteuser方法中创建的。为了创建这样的Principal对象,我需要来自httpsession的一些数据,因此我使用HttpSessionHandshakeInterceptor来填充上述方法中的映射属性 在我将应用程序打包从JAR(使用嵌入
Principal
实现,这是我在自定义DefaultHandshakeHandler->determinuteuser
方法中创建的。为了创建这样的Principal
对象,我需要来自httpsession的一些数据,因此我使用HttpSessionHandshakeInterceptor
来填充上述方法中的映射属性
在我将应用程序打包从JAR(使用嵌入式Tomcat)切换到WAR并将其部署到独立的Tomcat之前,一切都像一个符咒。突然,属性attributes
为空,就好像WS-connection使用的会话与HTTP连接不同,因此HttpSessionHandshakeInterceptor
不会传递所需的属性。有什么想法吗?为什么会有不同的表现
代码的几个部分:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/ws");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("ws-endpoint")
.setHandshakeHandler(new AnonymousHandshakeHandler())
.addInterceptors(new HttpSessionHandshakeInterceptor())
.withSockJS();
}
}
自定义握手处理程序:
public class AnonymousHandshakeHandler extends DefaultHandshakeHandler {
@Override
protected Principal determineUser(ServerHttpRequest request,
WebSocketHandler wsHandler, Map<String, Object> attributes) {
Principal principal = request.getPrincipal();
if (principal == null) {
SessionData sd = (SessionData) attributes.get(AppVariables.MODEL_PARAM_SESSION);
if (sd != null){
principal = new AnonymousPrincipal();
((AnonymousPrincipal) principal).setName(sd...);
}
}
return principal;
}
}
我解决了我的问题。它包括两个问题+我的愚蠢:
我正在使用
,但我忘记了。这就是为什么每个请求都会生成新会话(没有https的sameSite不会创建JSESSIONID cookie)
我的代理设置得不好。这就是为什么没有使用我的匿名握手器的原因。见:
public class HttpHandshakeInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map attributes) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession();
System.out.println(session.getId());
attributes.put(AppVariables.MODEL_PARAM_SESSION, session.getAttribute(AppVariables.MODEL_PARAM_SESSION));
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception ex) {
}
}