WebSocket握手期间出错:意外的响应代码:400 Spring boot websockets
Posted
技术标签:
【中文标题】WebSocket握手期间出错:意外的响应代码:400 Spring boot websockets【英文标题】:Error during WebSocket handshake: Unexpected response code: 400 Spring boot websockets 【发布时间】:2021-04-09 18:59:10 【问题描述】:我正在尝试实现 websocket。我的代码:
在 Spring Security 中,我允许使用身份验证的路径
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception
httpSecurity.csrf()
.disable()
.authorizeRequests()
.antMatchers("/register","/login","/testAuth","/web","/ws").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
以及为 ws 配置
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
registry.addHandler(new WsMessageHandler(), "/ws")
.addInterceptors(new CustomInterceptor())
.setAllowedOrigins("*")
.setHandshakeHandler(new DefaultHandshakeHandler())
.withSockJS();
`
public class CustomInterceptor implements HandshakeInterceptor
@Override
public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception
return true;
@Override
public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, @Nullable Exception e)
作为客户端,我使用简单的 js 代码:
var exampleSocket = new WebSocket("ws://localhost:8080/ws", "protocolOne");
exampleSocket.onopen = function()
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
;
但是我收到错误:
到 'ws://localhost:8080/ws' 的 WebSocket 连接失败:期间出错 WebSocket 握手:意外响应代码:400
这是什么原因造成的?一切都应该是正确的。谢谢解答
【问题讨论】:
【参考方案1】:我发现您的代码有一些错误:
-
如果你使用 WebSocket() 作为客户端,你应该在 WebSocketConfig 中注释 withSocktJS()。并喜欢它
@Configuration
@EnableWebSocket
public class WebSocketConfig2 implements WebSocketConfigurer
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
registry.addHandler(new WsMessageHandler(), "/ws")
.addInterceptors(new CustomInterceptor())
.setAllowedOrigins("*")
.setHandshakeHandler(new DefaultHandshakeHandler())
// .withSockJS()
;
-
您的客户端 js 代码错误。没有ws。您应该使用 exampleSocket 发送。如下所示
var exampleSocket = new WebSocket("ws://localhost:8081/ws", "protocolOne");
exampleSocket.onopen = function(ws)
console.log('-----------------', ws);
// Web Socket is connected, send data using send()
exampleSocket.send("Message to send");
alert("Message is sent...");
;
-
在客户端发送 websocket 时,您指向一个名为“protocolOne”的协议,您应该在 CustomInterceptor 中将其返回。您可以执行以下操作。
public class CustomInterceptor implements HandshakeInterceptor
@Override
public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception
return true;
@Override
public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, @Nullable Exception e)
if(serverHttpRequest.getHeaders() == null )
return ;
if(serverHttpRequest.getHeaders().get("Sec-WebSocket-Protocol") == null)
return ;
String protocol = (String) serverHttpRequest.getHeaders().get("Sec-WebSocket-Protocol").get(0);
if(protocol == null)
return ;
serverHttpResponse.getHeaders().add("Sec-WebSocket-Protocol", protocol);
【讨论】:
以上是关于WebSocket握手期间出错:意外的响应代码:400 Spring boot websockets的主要内容,如果未能解决你的问题,请参考以下文章
Java Glassfish Spring Sockjs失败:WebSocket握手期间出错:意外的响应代码:500