【发布时间】: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') 就可以了
【问题讨论】:
-
添加“socket.withCredentials = true ;”后就可以了
-
@a4fz067lu 你做了什么来解决这个问题?你在 Spring Boot 中向 CORS 配置添加了什么?