Hi! I am a beginner in Spring Boot. I have a simple Java Spring Boot Application which uses a Rest Controller. Some of the controller's endpoints are exposed to CORS requests. I've found out that it is possible to allow CORS requests by adding @CrossOrigin(origins ="..") annotations above the methods. My problem is that I don't know all the origins at the moment and will have to add some of them later during the runtime of my Spring Boot Application. Is it possible to have a method to manually add new origin-addresses to some endpoints of my Rest Controller? E.g. to have an endpoint in my Rest Controller that allows a particular user (e.g.Admin) to add new cors-origins to a rest endpoint? If yes: Please give a simple example.
I think this is possible by implementing your own org.springframework.web.cors.CorsProcessor
. Look at he implementation of org.springframework.web.filter.CorsFilter
which can get configured with your custom CorsProcessor
via method public void setCorsProcessor(CorsProcessor processor)
.
I woulod then suggest to implement your own CorsProcessor
and handle in the method process
your custom rules.
Or another option is to set your own CorsConfigurationSource
in your Spring Security Config. e.g.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().configurationSource(new CustomConfigSource());
}
The CorsConfigurationSource
offers a method CorsConfiguration getCorsConfiguration(HttpServletRequest request);
in which you can handle your specific CORS Handling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With