Java WebSocket 握手 - 意外响应代码 200 - AngularJs 和 Spring Boot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51845452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebSocket Handshake - Unexpected response code 200 - AngularJs and Spring Boot
提问by Heril Muratovic
When I tried to establish websocket communication between AngularJS app and Spring Boot I'm getting the error: Error during websocket handshake - Unexpected response code: 200.
当我尝试在 AngularJS 应用程序和 Spring Boot 之间建立 websocket 通信时,我收到错误:websocket 握手期间出错 - 意外响应代码:200。
Here is my JS code:
这是我的 JS 代码:
function run(stateHandler, translationHandler, $websocket) {
stateHandler.initialize();
translationHandler.initialize();
var ws = $websocket.$new('ws://localhost:8080/socket'); // instance of ngWebsocket, handled by $websocket service
ws.$on('$open', function () {
console.log('Oh my gosh, websocket is really open! Fukken awesome!');
});
ws.$on('/topic/notification', function (data) {
console.log('The websocket server has sent the following data:');
console.log(data);
ws.$close();
});
ws.$on('$close', function () {
console.log('Noooooooooou, I want to have more fun with ngWebsocket, damn it!');
});
}
And here is my Java code:
这是我的 Java 代码:
WebsocketConfiguration.java
WebsocketConfiguration.java
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
stompEndpointRegistry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
}
WebsocketSecurityConfiguration.java
WebsocketSecurityConfiguration.java
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages
// message types other than MESSAGE and SUBSCRIBE
.nullDestMatcher().authenticated()
// matches any destination that starts with /rooms/
.simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN)
.simpDestMatchers("/topic/**").permitAll()
// (i.e. cannot send messages directly to /topic/, /queue/)
// (i.e. cannot subscribe to /topic/messages/* to get messages sent to
// /topic/messages-user<id>)
.simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll()
// catch all
.anyMessage().denyAll();
}
Does anyone have an idea how to fix this problem?
Thank you in advance!
有谁知道如何解决这个问题?
先感谢您!
回答by Luka ?poljari?
I had a similiar problem, I was testing my websocket connection using an chrome plugin (Simple WebSocket Client) and was trying to connect to ws://localhost:8080/handler/ which is defined in my code as registry.addEndpoint("/handler").setAllowedOrigins("*").withSockJS();
but unexpected error 200 was occuring. I've fixed this by appending /websocket to my client request string on the chrome extension, so what you could try is to change in your JS file the following line:
我有一个类似的问题,我正在使用 chrome 插件(Simple WebSocket Client)测试我的 websocket 连接,并试图连接到 ws://localhost:8080/handler/,这是在我的代码中定义的, registry.addEndpoint("/handler").setAllowedOrigins("*").withSockJS();
但是发生了意外错误 200 . 我通过将 /websocket 附加到我的 chrome 扩展上的客户端请求字符串来解决这个问题,所以你可以尝试在你的 JS 文件中更改以下行:
var ws = $websocket.$new('ws://localhost:8080/socket');
to
到
var ws = $websocket.$new('ws://localhost:8080/socket/websocket');
I dont know the reason why this fixed it in my case i just randomly stumbled upon it, if some1 could clarify it more it would be really nice :)
我不知道为什么在我的情况下修复了它,我只是随机偶然发现它,如果有人能更清楚地说明它,那就太好了:)
回答by DiaMaBo
Spring Boot, WebSocket, and AngularI faced the same issue and this is how I did solve it and run websocket with real-time data crud ops in my angular stock blotters
Spring Boot、WebSocket 和 Angular我遇到了同样的问题,这就是我解决它的方法,并在我的 angular 股票记录仪中使用实时数据 crud ops 运行 websocket
.antMatchers("/ws/myapp", "/http/myaap", "/topic/**", "/app/**").permitAll()
.antMatchers("/ws/myapp", "/http/myaap", "/topic/**", "/app/**").permitAll()
WebSecurityConfigurerAdapter class
WebSecurityConfigurerAdapter 类
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/", "/favicon.ico")
.antMatchers("/api/stocks/**")
.permitAll()
.antMatchers("/ws/myapp", "/http/myaap", "/topic/**", "/app/**" )
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
AngularHere the urls to use in your service are these
Angular这里要在您的服务中使用的网址是这些
http://localhost:8080/http/myapp'
ws://localhost:8080/ws/myapp
I hope this will help
我希望这个能帮上忙
回答by Josef Vesely
Can you try this WebsocketConfigurationconfiguration:
你可以试试这个WebsocketConfiguration配置:
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*");
stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
}
so you have both websocket and SockJS configurations?
所以你有 websocket 和 SockJS 配置?
回答by Deadron
You need to use a sockJS client if you configure it to use sockJS on your websocket endpoint.
如果将 sockJS 客户端配置为在 websocket 端点上使用 sockJS,则需要使用 sockJS 客户端。