简体   繁体   English

Angular 和 Spring Boot:请求的资源上不存在“Access-Control-Allow-Origin”标头。 如何通过angular和spring boot解决

[英]Angular and Spring boot: No 'Access-Control-Allow-Origin' header is present on the requested resource. How to solve it by angular and spring boot

After all of this I am getting the same error .在这一切之后,我得到了同样的错误。 how to solve it.如何解决。

error: Access to XMLHttpRequest at 'http://localhost:8083/getuser' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource ##错误:从源 'http://localhost:4200' 访问 XMLHttpRequest at 'http://localhost:8083/getuser' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否 'Access -Control-Allow-Origin' 标头存在于请求的资源上##

Interceptor.ts拦截器

 @Injectable()
export class AuthInterceptor implements HttpInterceptor{

 constructor(private loginService:LoginService){

 }

 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  let newReq=req;
  let token=this.loginService.getToken();
  console.log("Interceptor ",token) 

  if(token!=null){
    newReq=newReq.clone({setHeaders:{Authorization:`Bearer ${token}`}})
  }
  return next.handle(newReq)
}

}

app.module.ts app.module.ts

providers: [LoginService,AuthGuard, 
 [{provide:HTTP_INTERCEPTORS,useClass:AuthInterceptor,multi:true}]]

Backend controller后端控制器

@CrossOrigin(origins ="*")
@RestController
public class Home {

@GetMapping("/welcome")  
public String welcome() {
    
    System.out.println("Home.welcome()");
    return "this is private page ";
}

@GetMapping("/getuser")
public String getUser() {
    
    return "My Name";
}
}

If this is just a toy project you are working with and not production code, you can just disable the CorsFilter in SpringSecurity like this ...如果这只是您正在使用的玩具项目而不是生产代码,您可以像这样禁用CorsFilter中的SpringSecurity ...

@EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.cors().disable()...
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 启动 OAuth 客户端配置给出“No 'Access-Control-Allow-Origin' header 存在于请求的资源上。” - Spring boot OAuth Client config giving "No 'Access-Control-Allow-Origin' header is present on the requested resource." angular 9 和 spring 启动 2 中请求的资源上不存在“访问控制允许来源”header - No 'Access-Control-Allow-Origin' header is present on the requested resource in angular 9 and spring boot 2 CORS 策略:请求的资源上不存在“访问控制允许来源”header Spring 引导 Rest API & VUE - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Spring Boot Rest API & VUE CORS 策略:请求的资源 Spring Boot Rest API 上不存在“Access-Control-Allow-Origin”标头 - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Spring Boot Rest API 请求的资源上不存在“Access-Control-Allow-Origin”header。 我在尝试从 Spring 数据 REST 获取数据时得到 - No 'Access-Control-Allow-Origin' header is present on the requested resource. I get when try to get data from Spring Data REST 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ xxx” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'xxx' is therefore not allowed access 在请求的资源上没有获取“ Access-Control-Allow-Origin”标头。 错误 - Getting No 'Access-Control-Allow-Origin' header is present on the requested resource. error CORS Spring Security 配置 - 404 请求的资源上不存在“Access-Control-Allow-Origin”标头 - CORS Spring Security configuration - 404 No 'Access-Control-Allow-Origin' header is present on the requested resource Java Spring:请求的资源上不存在“Access-Control-Allow-Origin”标头 - Java Spring: No 'Access-Control-Allow-Origin' header is present on the requested resource 春季文件上传失败,原因是请求的资源上存在“ Access-Control-Allow-Origin”标头 - Spring File Upload Fails by 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM