java - WebSockets 请求中请求的资源上不存在 'Access-Control-Allow-Origin' header

标签 java reactjs spring-mvc spring-boot cors

我有大量关于如何在 Java Spring 中配置 CORS 的示例,但它仍然无法在具有 websockets 请求的项目中工作。 它适用于 mcv api 路径,但我的 websockets 路径返回错误:

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:

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

也许有人有同样的错误或有任何解决方案我应该如何解决这个错误?

最佳答案

如果有人有这个问题,我在 WebsocketConfig 中解决了我的问题,如下所示:

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

关于java - WebSockets 请求中请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52385078/

相关文章:

reactjs - ScrollView 无法在 React Native 应用程序中启用滚动

reactjs - 如何让 React.memo 功能与 child 一起工作?

java - Spring MVC 3.1 RedirectAttributes 不工作

java - 带有 Spring MVC 4.0.1.RELEASE 的 Spring Security 3.2

java - spring mvc Hibernate 搜索查询

java - Session对象实例不同。它是否正确?

java - 从@OneToMany-association : CascadeType. ALL + orphanRemoval = true 中删除 child 不工作

arrays - React 将函数应用于数组中的单个项目

Java 泛型 : what does <? extends Interface <?>> 是什么意思?

java - Java中的正则表达式可以分割特殊字符和字母数字