我有大量关于如何在 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/