Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you completely disable CORS support in Spring?

As described in CORS preflight request fails due to a standard header if you send requests to OPTIONS endpoints with the Origin and Access-Control-Request-Method headers set then they get intercepted by the Spring framework, and your method does not get executed. The accepted solution is the use @CrossOrigin annotations to stop Spring returning a 403. However, I am generating my API code with Swagger Codegen and so I just want to disable this and implement my OPTIONS responses manually.

So can you disable the CORS interception in Spring?

like image 990
baynezy Avatar asked Jun 22 '17 11:06

baynezy


People also ask

What happens if we disable CORS?

As described in CORS preflight request fails due to a standard header if you send requests to OPTIONS endpoints with the Origin and Access-Control-Request-Method headers set then they get intercepted by the Spring framework, and your method does not get executed.

How do you prevent CORS from Spring boot?

To code to set the CORS configuration globally in main Spring Boot application is given below. Now, you can create a Spring Boot web application that runs on 8080 port and your RESTful web service application that can run on the 9090 port.

Is CORS enabled by default in Spring boot?

No. You need to add @CrossOrigin annotation by yourself to get CORS Support in Spring.


1 Answers

For newer versions of spring boot:

@Configuration public class WebConfiguration implements WebMvcConfigurer {      @Override     public void addCorsMappings(CorsRegistry registry) {         registry.addMapping("/**").allowedMethods("*");     } }  

The Kotlin way

@Configuration class WebConfiguration : WebMvcConfigurer {     override fun addCorsMappings(registry: CorsRegistry) {         registry.addMapping("/**").allowedMethods("*")     } } 
like image 179
Panthro Avatar answered Sep 19 '22 07:09

Panthro