WebSocket握手-意外的响应代码200-AngularJs和Spring Boot

时间:2018-08-14 15:57:23

标签: java angularjs spring websocket

当我尝试在AngularJS应用程序和Spring Boot之间建立Websocket通信时,出现错误: websocket握手期间出错-意外的响应代码:200

这是我的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!');
    });
}

这是我的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

@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();
}

有人知道如何解决此问题吗?
预先谢谢你!

4 个答案:

答案 0 :(得分:10)

我有一个类似的问题,我正在使用chrome插件(简单的WebSocket客户端)测试我的websocket连接,并尝试连接到ws:// localhost:8080 / handler /,在我的代码中将其定义为{{1 },但发生意外错误200。我已通过将/ websocket附加到chrome扩展名上的客户请求字符串中来解决此问题,因此您可以尝试在JS文件中更改以下行:

registry.addEndpoint("/handler").setAllowedOrigins("*").withSockJS();

var ws = $websocket.$new('ws://localhost:8080/socket');

我不知道为什么在我的情况下修复它的原因,我只是偶然发现了它,如果some1可以进一步阐明它,那将是非常好的:)

答案 1 :(得分:1)

您可以尝试以下 WebsocketConfiguration 配置吗?

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
    stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*");      
    stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
}

那么您既有websocket配置又有SockJS配置?

答案 2 :(得分:1)

如果将其配置为在websocket端点上使用sockJS,则需要使用sockJS客户端。

答案 3 :(得分:0)

Spring Boot,WebSocket和Angular 我遇到了同样的问题,这就是我的解决方法,并在有角度的吸墨纸机中使用实时数据操作来运行websocket

antMatchers("/ws/myapp", "/http/myaap", "/topic/**", "/app/**").permitAll()

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);
    }
}

角度 在您的服务中使用的网址是这些

http://localhost:8080/http/myapp'
ws://localhost:8080/ws/myapp 

我希望这会有所帮助