cors spring boot code example

Example 1: spring cors

public WebMvcConfigurer corsConfigurer() {
		return new WebMvcConfigurer() {
			@Override
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/greeting-javaconfig").allowedOrigins("http://localhost:9000");
			}
		};
	}Copy

Example 2: CORS with Spring Boot

@CrossOrigin

Example 3: cors filter spring boot not working in the tomcat web application


        cors
        com.udalmik.filter.CorsFilter
    

    
        cors
        /register
    

Example 4: how ro put value from application propertires in webmvc allowedOrigins

@SpringBootApplication
public class App extends SpringBootServletInitializer {

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(ReportsApplication.class, args);
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                String urls = env.getProperty("cors.urls");
                CorsRegistration reg = registry.addMapping("/api/**");
                for(String url: urls.split(",")) {
                    reg.allowedOrigins(url);
                }
            }
        };
    }    
}

Tags:

Misc Example