Spring CORS mappings

Configure CORS (Cross-origin resource sharing) for Spring Boot application


In this blog we will see how to configure CORS (Cross-origin resource sharing) for Spring Boot application. In the previous blog you can find the Spring Boot Hello World application. To configure the CORS we need to add following configuration class:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

public class WebConfig extends WebMvcConfigurerAdapter {

    public void addCorsMappings(CorsRegistry registry) {

                .allowedMethods("GET", "POST", "PATCH", "DELETE", "PUT", "OPTIONS")

So we have following configurations: (you can check full documentation here)

  • addMapping – Enable cross origin request handling for the specified path pattern.
  • allowedOrigins – Set the origins to allow, e.g. “http://domain1.com”. The special value “*” allows all domains.
  • allowedMethods – Set the HTTP methods to allow, e.g. “GET”, “POST”, etc. The special value “*” allows all methods.
  • allowedHeaders – Set the list of headers that a pre-flight request can list as allowed for use during an actual request. “*” it means everything is allowed. The special value “*” allows actual requests to send any header


You can learn more about the CORS here.