java Spring MVC proactively solves cross-domain request problems. Don’t rely on the front-end proxy to help you solve the problem, old man!

Since the development of the front-end, there have been many proxy tools that can help us solve cross-domain problems.
But in the final analysis, it is best if the back-end can solve cross-domain problems.

So let's look at a request first.
Insert image description here
It's obvious that this interface is cross-domain
because I'm using Spring MVC here
, and Spring MVC is very simple to solve cross-domain code.

We only need to add an annotation
@CrossOrigin.
The reference code is as follows

@CrossOrigin(origins = "*", allowedHeaders = "*")

First, CrossOrigin has two parameters. The first origins represents the allowed sources. Give an asterisk to represent all sources. The
second parameter allowedHeaders specifies an asterisk to indicate that there is no restriction
Insert image description here
on placing it on the class name. In this way, we are equivalent to letting go of all sources. limit

Then we start the project again
and then access the front-end service.
Insert image description here
You can see that the front-end successfully requested the data back this time.

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/133206673