Origin http://localhost:4200 has been blocked by CORS policy error in browser when tried to call Spring REST end point in angular

Viewed 1876

Getting below error when tried to call spring rest end point in angular.

Origin http://localhost:4200 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

Error Image: enter image description here

3 Answers

Solution: Add @CrossOrigin("*") annotation on top of respective Controller Class.

enter image description here

By using CrossOrigin("*") your accepting all domains.

which is not recommended and here is why .

Using @CrossOrigin(origins = "http://localhost:4200")

will have the same effect for your situation and would only accept localhost:4200 .

The best solution is to configure Angular to proxying call to the backend.

Take a look at Angular’s Proxing documentation

Related