参考了很多帖子,然后总是报空指针异常,最后从StackOverflow上得到的答案
1. 配置WebSocket
@Configuration
public class WebSocketConfig extends HttpSessionHandshakeInterceptor {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
2. 配置HttpSession
public class HttpSessionConfig extends ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
super.modifyHandshake(sec, request, response);
HttpSession httpSession = (HttpSession) request.getHttpSession();
sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
}
}
3. 添加过滤器
@Component
@ServletComponentScan
@WebFilter(filterN