[英]@CrossOrigin not working for spring-boot-starter-web 2.5.2
I have Spring Boot rest controller which exposes certain APIs which I want to consume in Angular 12. On the controller I have added @CrossOrigin, but spring-boot does not seem to pick it up because even after adding the annotation I am getting the Cors policy issue.我有 Spring Boot rest 控制器,它公开了我想在 Angular 12 中使用的某些 API。在控制器上我添加了 @CrossOrigin,但 spring-boot 似乎没有选择它,因为即使在添加注释后我也得到了 Cors政策问题。
@CrossOrigin(origins = "*")
@RestController
@Scope(WebApplicationContext.SCOPE_REQUEST)
@RequestMapping("api/rest")
public class MyController {
I think you've enabled the cross-origin for the particular controller.我认为您已经为特定控制器启用了跨域。 Either you can enable the same by adding the
@CrossOrigin(origins = "*")
on other controllers or you can configured the cross-origin globally by creating the bean as following您可以通过在其他控制器上添加
@CrossOrigin(origins = "*")
来启用相同的功能,或者您可以通过创建 bean 来全局配置跨域,如下所示
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
Have you tried adding it as @CrossOrigin(origins = "http://localhost:<your_port>")
您是否尝试将其添加为
@CrossOrigin(origins = "http://localhost:<your_port>")
Where your_port
is the port your angular app is using?其中
your_port
是您的角度应用程序使用的端口?
尝试以下代码:
@CrossOrigin(allowCredentials = "true")
Add a CORS-Origin extension to your browser if you are sending request from the browser.如果您从浏览器发送请求,请向浏览器添加 CORS-Origin 扩展。 If @CrossOrigin doesn't work then thats your problem.
如果@CrossOrigin 不起作用,那就是你的问题。 There you go:
你去吧:
https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.