What is the CORS

  1. CORS: Cross Origin Resource Share
  2. Origin should not be translated as domain, but as source. Origin contains protocols such as (HTTP/HTTPS); Domain names, such as www.baidu.com; Ports, if there are any ports
  3. The CORS problem is caused by the fact that the XMLHttpRequest and Fetch apis in browsers adhere to the same-Origin policy, but images, JS, and CSS do not

How does CORS work

  1. Add Http Headers to a Simple Request and let the server describe which source can read the server’s information
    1. Header: access-Control-allow-origin: * will appear in the server response
    2. Origin: www.example.com must be included in the browser request
  2. But the specification forces the browser to preflight a Request but is a way of getting permission from the server through an Option Request
    1. The browser sends the Option request first, carrying the additional Headers:
      1. Access-Control-Request-Method: POST
      2. Access-Control-Request-Headers: X-PINGOTHER, Content-Type

Server CORS configuration in Springboot

  1. You can configure Http Headers at either the method level or the class level with @Crossorigin
  2. The CorsRegistory class can be uniformly configured in the configuration class
  3. IO /guides/gs/ R…