Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate Gateway Gateway to solve Cross-domain problems

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to integrate Gateway gateways to solve cross-domain problems". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. introduction of Gateway Gateway

The API gateway is the middle layer between the client and the server, and all external requests will pass through the API gateway first. In other words, the implementation of API pays more attention to business logic, while security, performance and monitoring can be done by API gateway, which not only improves business flexibility but also has no lack of security.

Spring cloud gateway is an official gateway developed by spring based on Spring 5.0, Spring Boot2.0 and Project Reactor. Spring Cloud Gateway aims to provide simple, effective and unified API routing management for micro-service architectures. Spring Cloud Gateway, as a gateway in Spring Cloud ecosystem, aims to replace Netflix Zuul. It not only provides a unified routing method, but also provides the basic functions of the gateway based on Filer chain. For example: safety, monitoring / burying point, current restriction and so on.

Step 1: build the module and introduce dependencies in Gateway

The Maven module is used in the project to build the server-gateway module.

Since Gateway also needs to be registered in Nacos, Gateway dependencies should be introduced as well as Naocs dependencies.

Org.springframework.cloud spring-cloud-starter-gateway com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery step 2: add a profile

The following configuration indicates that after setting the gateway service port to 9000, two routing id are configured:

# Service port server.port=9000# service name spring.application.name=service-gateway# nacos service address spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848# use service discovery route spring.cloud.gateway.discovery.locator.enabled=true# set route idspring.cloud.gateway.routes [0] .id=service-hosp# set route urispring.cloud.gateway.routes [0] .uri=lb://service-hosp# set route assertion Proxy servicerId sets the route idspring.cloud.gateway.routes for the / auth/ path spring.cloud.gateway.routes [0] .uri = Path=/*/hosp/**# of auth-service [1] .id=service-cmn# sets the route urispring.cloud.gateway.routes [1] .uri = lb://service-cmn# sets the route assertion / auth/ path spring.cloud.gateway.routes [1] .initiates = Path=/*/cmn/** where the agent servicerId is auth-service step 3: create a startup class @ SpringBootApplicationpublic class ServerGatewayApplication {public static void main (String [] args) {SpringApplication.run (ServerGatewayApplication.class, args) }} preliminary test of gateway

After the backend service is configured, start the corresponding micro service. In the frontend dev.env.js file, set the port to 9000 to correspond to the backend:

'use strict'const merge = require (' webpack-merge') const prodEnv = require ('. / prod.env') module.exports = merge (prodEnv, {NODE_ENV:'"development", BASE_API:'"http://localhost:9000"',})

If you close the previously configured Nginx and send a request before different ports, you can access it successfully:

Third, Gateway solves cross-domain problems

Create a configuration class in the service_gateway module and write the following:

@ Configurationpublic class CorsConfig {@ Bean public CorsWebFilter corsFilter () {CorsConfiguration config = new CorsConfiguration (); config.addAllowedMethod ("*"); config.addAllowedOrigin ("*"); config.addAllowedHeader ("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (new PathPatternParser ()); source.registerCorsConfiguration ("/ *", config); return new CorsWebFilter (source);}}

Once configured, remove the @ CrossOrigin annotations from all Controller in the previous project.

Start the back-end micro-service and the front-end project again, switch different tabs, and find that the cross-domain problem has been successfully solved:

This is the end of the content of "how to integrate Gateway gateways to solve cross-domain problems". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report