WebSockets请求中的请求资源上不存在“ Access-Control-Allow-Origin”标头 [英] No 'Access-Control-Allow-Origin' header is present on the requested resource in WebSockets request

查看:357
本文介绍了WebSockets请求中的请求资源上不存在“ Access-Control-Allow-Origin”标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多关于如何在Java Spring中配置CORS的示例,但是在websockets请求的项目中,它仍然不起作用。
它适用于mcv api路径,但我的websockets路径返回错误:

I have red tons of example how to configure CORS in Java Spring, but it is still not working in project with websockets request. It works with mcv api paths, but my websockets path's returns error:


无法加载 http:// localhost:8080 / chat / info?t = 1537264329515 :否
'访问控制-Allow-Origin'标头出现在请求的
资源上。因此,不允许
访问源‘ http:// localhost:3000 。响应的HTTP状态码为403。

Failed to load http://localhost:8080/chat/info?t=1537264329515: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.

我的WebMvcConfig:

My WebMvcConfig:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer
{
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("http://localhost:3000");
    }
}

也许有人遇到相同的错误或有任何解决方案我解决了这个错误?

Maybe someone had same error or have any solutions how should I solve this error?

推荐答案

如果有人会有这个问题,我可以这样在WebsocketConfig中解决我的问题:

If someone will have this question, I solved my problem in WebsocketConfig like this:

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/chat").setAllowedOrigins("http://localhost:3000").withSockJS();
}

这篇关于WebSockets请求中的请求资源上不存在“ Access-Control-Allow-Origin”标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆