Spring 带有VueJs的弹簧腹板箱-被CORS阻止

Spring 带有VueJs的弹簧腹板箱-被CORS阻止,spring,vue.js,websocket,Spring,Vue.js,Websocket,我正在试图找到一个关于如何通过WebSocket连接VueJs和Spring的解决方案 WebSocket形态(春季) @覆盖 公共无效注册表TompendPoints(StompEndpointRegistry注册表){ registry.addEndpoint(“/gs-guide-websocket”) .setAllowedOrigins(“http://localhost:8080") .withSockJS(); } 我用.setAllowedOriginates(“”)尝试了几个

我正在试图找到一个关于如何通过WebSocket连接VueJs和Spring的解决方案

WebSocket形态(春季)

@覆盖
公共无效注册表TompendPoints(StompEndpointRegistry注册表){
registry.addEndpoint(“/gs-guide-websocket”)
.setAllowedOrigins(“http://localhost:8080")
.withSockJS();
}
我用
.setAllowedOriginates(“”
)尝试了几个选项,比如“*”、“”或不使用它

我的项目中也有CORS配置

@Bean
公共公司配置源公司配置源(){
CorsConfiguration配置=新的CorsConfiguration();
configuration.setAllowedOriginates(Arrays.asList(“http://localhost:8080"));
setAllowedMethods(Arrays.asList(“GET”、“POST”、“PUT”、“PATCH”、“DELETE”、“OPTIONS”));
setAllowedHeader(Arrays.asList(“授权”、“内容类型”、“x-auth-token”));
setExposedHeaders(Arrays.asList(“x-auth-token”);
UrlBasedCorsConfigurationSource=新的UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration(“/**”,配置);
返回源;
}
在我的CORS配置中,我尝试了许多选项,但没有结果:(

最后是客户端连接(VueJs):

connect(){
this.socket=新的SockJS(“http://localhost:8081/gs-导向网袋);
this.stompClient=Stomp.over(this.socket);
this.stompClient.connect(
{},
帧=>{
this.connected=true;
控制台日志(框架);
this.stompClient.subscribe(“/topic/chat/1”,勾选=>{
控制台日志(勾选);
this.received_messages.push(JSON.parse(tick.body).content);
});
},
错误=>{
console.log(错误);
this.connected=false;
}
);
}
在我尝试过的每一件事上,我都会遇到这样的错误:

CORS策略已阻止从源“”访问“”处的XMLHttpRequest:响应中“访问控制允许凭据”标头的值为“”,当请求的凭据模式为“包括”时,该值必须为“真”。XMLHttpRequest启动的请求的凭据模式由withCredentials属性控制。

有什么解决办法可以让它工作吗

供参考:

  • Spring:
    localhost:8081
  • VueJs:
    localhost:8080

谢谢大家的帮助:)

我离得太近了。这里的一切都是对的(当然不是为了生产)

CORS conf中缺少的最后一件事是将
Allow credentials
设置为
true


我希望这篇文章能对你有所帮助:)

我离你太近了。这里的一切都是对的(当然不是为了生产)

CORS conf中缺少的最后一件事是将
Allow credentials
设置为
true

我希望这篇文章能对你有所帮助:)