【发布时间】:2019-09-25 06:48:36
【问题描述】:
我想将 Spring Boot 应用程序与 Angular 应用程序集成。两个应用程序都在不同的服务器上运行。当我尝试从 Angular 应用程序 API 成功命中 REST API 但我在 UI 上收到错误响应时。
从源“http://localhost:4200”访问“http://localhost:9092/jobs/postal/launch”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头出现在请求的资源上。
我在后端实现了 CORS 协商。所以我只希望我能够在没有任何安全性的情况下从 UI 访问后端。
我还阅读了以下文章以了解 CORS 和 CSRF。
https://markitzeroday.com/x-requested-with/cors/2017/06/29/csrf-mitigation-for-ajax-requests.html
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings( CorsRegistry registry) {
registry.addMapping(corsMapping)
.allowedOrigins(allowedOrigins)
.allowedMethods(allowedMethods).allowedHeaders("X-Requested-With");
}
};
}
我只是想知道如何才能成功地将 Spring Boot 应用程序与 Angular 应用程序集成。
如果有任何文档或链接可以帮助我,请告诉我。
【问题讨论】:
标签: java angular spring-boot spring-security