Springboot和Angular Websocket的CORS问题

问题描述 投票:0回答:2

我有两个服务来构建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的角服务

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

我已经尝试过

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

或使用CORS Anywhere in javascript

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

以及最佳方法是什么?>

我应该对后端服务器进行角度代理吗?

或者通过setAllowedOrigins('host:port')没关系

我有两个服务来构建一个Spring Boot应用程序,但是我总是遇到类似CORS的问题,例如,从来源地'http:// localhost:8080'在'... / websocket-cr / info?t = 1581585481804'处对XMLHttpRequest的访问。 ...

spring websocket sockjs
2个回答
0
投票

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


0
投票

只需在类顶部添加@ CrossOrigin

© www.soinside.com 2019 - 2024. All rights reserved.