Set CORS in spring boot

Set CORS in spring boot
spring-boot CORS logo

We can set CORS in spring boot at below levels

      1. At the Application level
      2. At a controller/method level

For instance to set CORS in spring boot at the application level, do this~

      1. First let us create a bean object at the @SpringBootApplication class with the below code

                @Bean
        	public WebMvcConfigurer corsConfigurer() {
        		return new WebMvcConfigurer() {
        			@Override
        			public void addCorsMappings(CorsRegistry registry) {
        				registry.addMapping("**/**").allowedOrigins("http://localhost:8888");
        			}
        		};
        	}
                    

        Now therefore the @ SpringBootApplication class will look like this

        package com.codegigs.app.rws;
        
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.context.annotation.Bean;
        // also need to import the CorsRegistry
        import org.springframework.web.servlet.config.annotation.CorsRegistry;
        import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
        
        @SpringBootApplication
        public class RwsApplication {
        
        	public static void main(String[] args) {
        		SpringApplication.run(RwsApplication.class, args);
        	}
        
        	//Adding application level CORS
        	@Bean
        	public WebMvcConfigurer corsConfigurer() {
        		return new WebMvcConfigurer() {
        			@Override
        			public void addCorsMappings(CorsRegistry registry) {
        				registry.addMapping("**/**").allowedOrigins("http://localhost:8888");
        			}
        		};
        	}
        }
        
        

      2. Similarly we can set CORS in spring boot at the Controller class level or at the method level.

        For instance let us add the below annotation before the beginning of our @RestController class.

        This annotation will set CORS in spring boot at a controller class level.

        1. In addition, we can set CORS at a Controller(class) level.
          To do that we need to use the below approach. Here we are applying the CORS at the Controller class .

          <strong>@CrossOrigin(origins = "https://codegigs.app", maxAge = 3600)
          @RestController</strong>
          ...
          ...
          
        2. Similarly we can set CORS at a Controller method level.

          For this we shall be applying the @CrossOrigin annotation at the beginning of the method like below.

          
          @CrossOrigin(maxAge = 3600)
          @RestController
          @RequestMapping("/account")
          public class ShoppingCartController {
          
          	<strong>@CrossOrigin</strong>(origins = "https://codegigs.app")
          	@GetMapping("/{id}")
          	<strong>public Cart retrieve(@PathVariable Long id) {</strong>
          		// ...
          	}
          
          
          
Doubts? WhatsApp me !