Springboot和角形Websocket的CORS问题
我有两个服务来构建Spring引导应用程序 但我总是遇到这样的问题Springboot和角形Websocket的CORS问题,spring,websocket,sockjs,Spring,Websocket,Sockjs,我有两个服务来构建Spring引导应用程序 但我总是遇到这样的问题 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
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服务
8080上的角度服务
我试过了
setAllowedOrigins("http://localhost:8080").withSockJS();
setAllowedOrigins("*").withSockJS();
或者在javascript中的任意位置使用CORS
最好的方法是什么
我应该将angular proxy设置为后端服务器吗
或者在SpringBoot服务的主类中设置AllowedOrigins'host:port'就可以了,注入下面的bean,它就可以工作了
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer () {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*")
.allowedOrigins("*");
}
};
}
在SpringBoot服务的主类中,注入下面的bean,它就会工作
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer () {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*")
.allowedOrigins("*");
}
};
}
只需在类的顶部添加@CrossOrigin注释即可。这工作做得很好。例如:
@RestController
@CrossOrigin(origins = "*")
public class YourController {
.....
}
只需在类的顶部添加@CrossOrigin注释即可。这工作做得很好。例如:
@RestController
@CrossOrigin(origins = "*")
public class YourController {
.....
}
抱歉来晚了。 我在这里使用的是带springboot的angular 8中的STOMP JS 如果需要,您需要添加WebSocketConfig类来分别为套接字和控制器配置内容 配置类 参考号 在控制器类中,只需添加
@Controller
@CrossOrigin(origins = "*")
public class LogsController {
..
}
答案将被更新以进行身份验证/授权。很抱歉来晚了。 我在这里使用的是带springboot的angular 8中的STOMP JS 如果需要,您需要添加WebSocketConfig类来分别为套接字和控制器配置内容 配置类 参考号 在控制器类中,只需添加
@Controller
@CrossOrigin(origins = "*")
public class LogsController {
..
}
答案迟早会更新以进行身份验证/授权。在我添加socket之后就可以工作了。withCredentials=true;ref在我添加socket.withCredentials=true之后它是工作;参考参考参考
@Controller
@CrossOrigin(origins = "*")
public class LogsController {
..
}