Enabling Cross Origin Requests for WebSockets in Spring

前端 未结 2 1032
长发绾君心
长发绾君心 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

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

提交回复
热议问题