简体   繁体   English

@CrossOrigin 不适用于 spring-boot-starter-web 2.5.2

[英]@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 {

cors error错误

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.

相关问题 @CrossOrigin无法解析-Spring Boot - @CrossOrigin cannot be resolved - Spring boot 将 spring spring-boot-starter-oauth2-client 与 angular 一起使用 - Use spring spring-boot-starter-oauth2-client with angular 在控制器级别使用 @CrossOrigin 使用 Angular 运行 Spring Boot 时出现 CORS 问题 - CORS issue when running Spring Boot with Angular using @CrossOrigin at controller level Spring Boot:即使在使用 @CrossOrigin 时,CORS 策略也阻止了从源 FRONTEND 在 BACK_END&#39; 处访问 XMLHttpRequest - Spring Boot : Access to XMLHttpRequest at BACK_END' from origin FRONTEND has been blocked by CORS policy even when @CrossOrigin is used Spring boot + Angular应用程序正在运行 - Spring boot + Angular application working Spring Boot Web应用程序中的Angular 2文件 - Angular 2 Files within Spring Boot Web Application 如何从直播中获取视频并将其传递到本地网络而无需跨域 - How to get video from livestream and pass it to local web without crossorigin Spring Boot Jhipster映像上传不起作用 - Spring boot Jhipster image upload not working Spring Boot - 会话超时在 2.2.1 版本中不起作用 - Spring Boot - Session timeout not working in 2.2.1 version Angular - Maven/Spring Boot CORS 不工作 - Angular - Maven/Spring Boot CORS not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM