赞
踩
浏览器控制台错误提示
Error during WebSocket handshake: Unexpected response code: 403
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(socketHandler, "/test").addInterceptors(new SystemInfoSocketHandshakeInterceptor());
registry.addHandler(socketHandler, "/sockjs/test").addInterceptors(new SystemInfoSocketHandshakeInterceptor())
.withSockJS();
}
@Configuration public class SystemInfoSocketHandshakeInterceptor implements HandshakeInterceptor { @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object > attributes) { log.info("socket beforeHandshake.."); if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; HttpSession session = servletRequest.getServletRequest().getSession(false); // 业务处理 } return true; } @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { log.info("socket beforeHandshake.."); } }
var wsServer = "ws://127.0.0.1:8080";
var webSocket;
if ('WebSocket' in window || 'MozWebSocket' in window) {
webSocket = new WebSocket(wsServer + "/test");
} else {
webSocket = new SockJS(wsServer + "/sockjs/test");
}
webSocket.onerror = function (event) {
console.log("websockt连接错误")
};
如果是连接上面的问题,一般看一下配置是否正确
注意版本问题
在拦截器中,解除拦截
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
在nginx中配置
proxy_set_header Upgrade $http_upgrade
proxy_set_header Connection "upgrade"
proxy_set_header Host $host
三个配置
完整配置如下:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
添加setAllowedOrigins("*")
解决跨域问题
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(socketHandler, "/test")
.addInterceptors(new SystemInfoSocketHandshakeInterceptor())
.setAllowedOrigins("*");
registry.addHandler(socketHandler, "/sockjs/test")
.addInterceptors(new SystemInfoSocketHandshakeInterceptor())
.setAllowedOrigins("*")
.withSockJS();
}
收录时间: 2021/02/03
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。