【问题标题】:CORS problems with Springboot and Angular WebsocketSpringboot 和 Angular Websocket 的 CORS 问题
【发布时间】:2020-02-13 10:11:57
【问题描述】:

我有两个服务来构建一个 Spring Boot 应用程序

但我总是遇到这样的 CORS 问题

Access to XMLHttpRequest at '.../websocket-cr/info?t=1581585481804' from origin'http://localhost:8080' 
has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin'
header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. 
The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials.

8082 上的 Springboot 服务

    public void registerStompEndpoints(StompEndpointRegistry registry) {
       registry.addEndpoint("/websocketcr").
       setAllowedOrigins("http://localhost:8080").withSockJS();
    }

8080 上的 Angular 服务

    var socket = new SockJS('http://localhost:8080/websocket-cr');
    //socket.withCredentials = true ;
    stompClient = Stomp.over(socket);

我试过了

    setAllowedOrigins("http://localhost:8080").withSockJS();
    setAllowedOrigins("*").withSockJS();

或在javascript中使用CORS Anywhere

    var socket = new SockJS('http://cors-anywhere.herokuapp.com/http://localhost:8082/websocket-cr');
    socket.withCredentials = true ;

最好的方法是什么

我应该为我的后端服务器制作角度代理吗?

或者 setAllowedOrigins('host:port') 就可以了

【问题讨论】:

标签: spring websocket sockjs


【解决方案1】:

在 SpringBoot 服务的 Main 类中,注入下面的 bean,它将起作用

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer () {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*")
                    .allowedOrigins("*");
        }
    };
}

【讨论】:

    【解决方案2】:

    很抱歉参加这个聚会迟到了。 我在angular 8springboot 中使用STOMP JS working demo 如果需要,您需要添加WebSocketConfig 类来分别配置 Socket 和 For 控制器。

    配置类

    import org.springframework.context.annotation.Configuration;
    import org.springframework.messaging.simp.config.MessageBrokerRegistry;
    import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
    import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
    import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
    
    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    
        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            config.enableSimpleBroker("/topic");
            config.setApplicationDestinationPrefixes("/app");
        }
    
        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/ws")
            .setAllowedOrigins("*")
            //.setAllowedOrigins("http://localhost:4200").setAllowedOrigins("http://localhost:8081")
            .withSockJS();
        }
    }
    

    参考Another Help from Spring people

    Controller类中添加

    @Controller
    @CrossOrigin(origins = "*")
    public class LogsController {
    ..
    }
    

    并且迟早会更新答案以进行身份​​验证/授权。

    【解决方案3】:

    只需在类顶部添加 @CrossOrigin 注释即可。这是完美的工作。例如:

    @RestController
    @CrossOrigin(origins = "*")
    public class YourController {
        .....
    }
    

    【讨论】:

      猜你喜欢
      • 2023-02-13
      • 2019-06-11
      • 2015-05-13
      • 2018-09-12
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      相关资源
      最近更新 更多