The solution of cross domain problem of front end implemented by spring cloud

When we need to provide spring boot with external services in the form of restful interface, if the architecture is separated from the front and back, then the cross domain problem will be involved. How to solve the cross domain problem is discussed below.

Solution 1: add @ crossorigin annotation on the controller

The usage is as follows:

Solution 2: global configuration

The code is as follows:

Solution 3: combined with filter

In the main class of spring boot, add a corsfilter

Of course, if there are many microservices, you need to add such a piece of code to the main class of each service, which violates the dry principle. A better way is to solve the cross domain problem in zuul's gateway layer once and for all.

For more information on front-end cross domain, refer to: https://www.oudahe.com/p/41208/

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>