This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19816|Reply: 0

[Source] Spring Cloud Gateway CORS cross-domain configuration

[Copy link]
Posted on 7/28/2021 5:39:14 PM | | | |
The project is the separation of the front and back ends, the front end and the back end are not on the same port, and there is a cross-domain problem when the front end calls the backend interface.

If the gateway allows cross-domain transmission, the following error is reported:

Access to XMLHttpRequest at 'http://127.0.0.1:9999/gateway/auth/account/requestToken' from origin 'http://localhost:60356' 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.
jquery.min.js:2 POST http://127.0.0.1:9999/gateway/auth/account/requestToken net::ERR_FAILED
About the Spring Cloud Gateway CORS configuration document tutorial:The hyperlink login is visible.

Modify the application.yml configuration file as follows:

Once the setup is complete, rerun the project due toThe microservices themselves are also set up to allow cross-domain, duplicate content will appear in the POST response header with the following error:

Access to XMLHttpRequest at 'http://127.0.0.1:9999/gateway/auth/account/requestToken' from origin 'http://localhost:60356' has been blocked by CORS policy: The ' Access-Control-Allow-Origin' header contains multiple values 'http://localhost:60356, http://localhost:60356', but only one is allowed.


Solution: Create a new CorsResponseHeaderFilter filter to remove some of the same response headers, the code is as follows:

Create a new CorsConfiguration file and register the beans with the following code:

Successfully resolve the issue as shown in the image below:



(End)





Previous:CentOS 7 copies files to another server via commands
Next:Use Visual Studio to compare text differences
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com