简体   繁体   English

将Spring Boot应用程序与Angular应用程序集成

[英]Integration of Spring Boot application with Angular application

I want to integrate Spring Boot application with Angular application. 我想将Spring Boot应用程序与Angular应用程序集成在一起。 Both applications are running on a different server. 这两个应用程序都在不同的服务器上运行。 When I will try to hit REST API from the Angular application API hit successfully but I get an error response on UI. 当我尝试从Angular应用程序API中成功单击REST API时,但在UI上收到错误响应。

Access to XMLHttpRequest at ' http://localhost:9092/jobs/postal/launch ' 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 '在' http:// localhost:9092 / jobs / postal / launch '处对XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:否“ Access-Control-Allow-Origin”标头存在于请求的资源上。

I implemented the CORS negotiation on the backend side. 我在后端实施了CORS协商。 So I just want that I am able to hit the backend from UI without any security. 因此,我只希望我能够在没有任何安全性的情况下从UI进入后端。

I also read the below article to get an understanding of CORS and CSRF. 我还阅读了以下文章,以了解CORS和CSRF。

https://markitzeroday.com/x-requested-with/cors/2017/06/29/csrf-mitigation-for-ajax-requests.html 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");
        }
    };
}

I just want to know how can I get successfully integrate Spring Boot application with Angular application. 我只想知道如何才能成功地将Spring Boot应用程序与Angular应用程序集成。

If there is any doc or link that can help me please let me know. 如果有任何文档或链接可以帮助我,请告诉我。

CORS manages the access to the requests originating from the servers other than where backend application is deployed. CORS管理对源自服务器(而不是部署后端应用程序)的服务器的请求的访问。 By-default it is set to only the backend hosting server due to security reasons which means only requests originating from backend hosting server are allowed. 由于安全原因,默认情况下将其设置为仅后端托管服务器,这意味着仅允许来自后端托管服务器的请求。

In your case since both backend and UI are running on separate servers you need to add some code changes in backend to allow the UI to access the backend. 在您的情况下,由于后端和UI都在单独的服务器上运行,因此您需要在后端添加一些代码更改,以允许UI访问后端。

You can have a class ResourceApplication.java as shown below: 您可以拥有一个ResourceApplication.java类,如下所示:

@RequestMapping("/")
@CrossOrigin(origins="*", maxAge=3600)
public Message home() {
  return new Message("Hello World");
}

The above is taken from this tutorial which is provided by spring.io and is an excellent guide if you are integrating spring-boot with angular. 以上内容摘自spring.io提供的教程,如果您将spring-boot与angular集成在一起,它是一个很好的指南。 If you scroll down you will find the CORS section. 如果向下滚动,则会找到“ CORS”部分。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM