Enabling Cross Origin Requests for WebSockets in Spring

前端 未结 2 963
长发绾君心
长发绾君心 2021-02-04 08:24

I have a OpenShift Wildfly server. I am building a website with the Spring MVC framework. One of my webpages also uses a WebSocket connection. On the server side, I

相关标签:
2条回答
  • 2021-02-04 08:51

    I don't think it's a CORS issue because it's connected successully before being disconnected. If that's CORS, you can't even connect.

    I think it's a communication problem between your DNS & openshift because WebSocket need a persistent connection (long-live) which keeps opening between client & server. If your DNS (e.g. CloudFlare or something like that) does not support / not configured to use WebSocket, the client would be disconnected immediately as in your issue.

    0 讨论(0)
  • 2021-02-04 08:51

    Have you tried implementing the WebMvcConfigurer and overriding the method addCorsMappings()? If not try this and see.

    @EnableWebMvc
    @Configuration
    @ComponentScan
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
    
            registry.addMapping("/**")
            .allowedOrigins("*")
            .allowedMethods("GET", "POST")
            .allowedHeaders("Origin", "Accept", "Content-Type", "Authorization")
            .allowCredentials(true)
            .maxAge(3600);
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题