Adding a CORS policy
We have learned how CORS works and the advantage it brings to the security of our applications. Now, we will configure and implement a CORS policy in our Spring Boot project.
There are several ways to configure CORS on our project. We will discuss them one by one.
CORS applications for each method
We can enable CORS on a single endpoint; this means that we can specify different permitted origins for other endpoints. Let’s have a look at the following example:
@CrossOrigin @GetMapping public List<AntiHeroDto> getAntiHeroes(Pageable pageable) { ..code implementation }
In our Spring Boot project, we have the getAntiHeroes()
method. To enable CORS on a specific method, we will use the @CrossOrigin
annotation. We can see that we have not configured any other settings, and this applies the following:
- All origins are permitted.
- HTTP methods that are allowed are the ones configured for the method (in this method, the...