1. What is a cross-domain
Cross-domain, or cross-site HTTP requests (Cross-site HTTP request), refers to the domain in which the requested resource originated differs from the request to the HTTP request pointing to the domain of the resource.
2. Cross-domain application scenarios
When using front-end separation and backend-led development in front-end collaborative development, there are often the following scenarios:
- Backend development is completed on the server and deployed to the front-end API documentation.
- The front-end is developed locally and requests are sent to the backend deployed on the remote server.
In this development process, if the front end wants to test the interface while developing, it needs to use a cross-domain approach.
3. Annotate to allow cross-domain
Very simple, we can annotate the controller class or its methods @CrossOrigin
to enable it to support cross-domain.
Example:
@CrossOrigin(origins = "*", maxAge = 3600)@RestController@RequestMapping("/User")public class UserController {}
Where origins is the default parameter for Crossorigin, that is, the cross-domain source, * that is, any source, or another domain name. That can be in the following form:
@CrossOrigin("http://test.com")@CrossOrigin(origins="http://test.com",maxAge=3600)
This annotation is used on methods, the same way, and when processed, SPRINGMVC the label on the class and on the method.
Solving the problem of Ajax cross-domain requests using @crossorigin annotations in SPRINGMVC