22

I have implemented WebSockets with Spring Boot Application and have the below error message when trying to test the ws connection with the chrome extension 'Smart websocket client'. However, I have no problem when run the Spring Boot Application locally.

WebSocket connection to 'ws://192.168.X.XYZ:8080/test' failed: 
Error during WebSocket handshake: Unexpected response code: 403

The only difference which I see is in the Request headers:

In the one it works - Origin:http://192.168.X.XYZ:8080

In the one it does not work - Origin:chrome-extension://omalebghpgejjiaoknljcfmglgbpocdp

What I did in the WebSocketConfig class is below:

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(myHandler(), "/test").setAllowedOrigins("http://192.168.X.XYZ:8080");
}

and still does not work.

Could you please advise what the reason for that error might be and how to fix it?

Thank you in advance.

2 Answers 2

20

You need to configure your "chrome-extension://..." origin as an allowed origin or even "*", otherwise it's rejected by the server.

1
  • 11
    setAllowedOrigins("*"); fixed the issue. Thanks!
    – M.Bel
    Sep 22, 2016 at 13:05
1

On update to spring boot 2.4, it also requires:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("*");
        }
    };
}

and @EnableAutoConfiguration on the config class.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.