springboot No 'Access-Control-Allow-Origin' header is present on the requested resource

springboot

“No ‘Access-Control-Allow-Origin’ header is present on the requested resource” is an error that occurs when a web browser is trying to access a resource from a different domain than the one that served the original page. This error occurs because of the same-origin policy, which is a security feature implemented in web browsers that blocks web pages from making requests to a different domain than the one that served the original page.

To fix this error, you will need to add the “Access-Control-Allow-Origin” header to the resource that you are trying to access. This can be done in a few different ways, depending on your setup:

  1. If you have control over the server that is serving the resource, you can add the “Access-Control-Allow-Origin” header to the HTTP response. For example, in Spring Boot, you can do this by adding the following code to your controller:
@CrossOrigin(origins = "http://example.com")
@GetMapping("/resource")
public ResponseEntity<Object> getResource() {
    // code to retrieve and return resource
}
  1. If you do not have control over the server that is serving the resource, you can use a browser extension or plugin to add the “Access-Control-Allow-Origin” header to the HTTP request. There are several extensions and plugins available for different web browsers that allow you to do this.