Spring WebSocket Connecting with SockJS to a different domain

前端 未结 4 1399
有刺的猬
有刺的猬 2021-02-02 09:30

WebSockets in Spring is a rather new topic that I;m tiring to find a bit more.

My problem is with connecting to a service from a different domain, I\'m working on with

4条回答
  •  没有蜡笔的小新
    2021-02-02 09:45

    In my case, I had to add these configuarations to get SockJS / STOM to work with CORS:

    @Configuration
    @EnableWebMvc
    public class WebConfig implements WebMvcConfigurer
    {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowCredentials(false)
                    .maxAge(3600)
                    .allowedHeaders("Accept", "Content-Type", "Origin", 
    "Authorization", "X-Auth-Token")
                    .exposedHeaders("X-Auth-Token", "Authorization")
                    .allowedMethods("POST", "GET", "DELETE", "PUT", "OPTIONS");
        }
    }
    

提交回复
热议问题