Spring Boot enabling CORS with application.properties

I am using the Spring Boot RESTful API to automatically insert your entity class. I use this apiRest from a front-end web application, but it gives me this error:

The requested resource is missing the header "Access-Control-Allow-Origin"

I am setting up the CORS configuration using the applicationantion.properties properties listed here .

My main configuration:

endpoints.cors.allow-credentials=true
endpoints.cors.allowed-origins=*
endpoints.cors.allowed-methods=*
endpoints.cors.allowed-headers=*

I tried different combinations in these variables, but still not working. Any ideas?

+15
source share
6 answers

I got the answer myself:

Just add this to application.java

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

Spring, Spring

, CORS, application.properties. JavaConfig, .

@EnableWebMvc @Configuration, WebMvcConfigurerAdapter addCorsMappings :

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
            .allowedOrigins("http://domain2.com")
            .allowedMethods("PUT", "DELETE")
            .allowedHeaders("header1", "header2", "header3")
            .exposedHeaders("header1", "header2")
            .allowCredentials(false).maxAge(3600);
    }
}
+20

Spring , endpoints.cors. * Actuator, MVC.

+9

URL, cors, application.properties. Spring Boot 5.

App.java ( ):

@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);
                }
            }
        };
    }    
}

application.properties:

cors.urls=http://localhost:3000
+3

spring boot2, URL- ,

management.endpoints.web.cors.allowed-origins=http://examaple.com ,http://examaple1.com 
management.endpoints.web.cors.allowed-methods=GET, POST
0

Spring-Boot 2.1.2:

, . "Access-Control-Allow-Origin". CorsFilter, Spring.

@Bean
public CorsFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("http://127.0.0.1:4200");
    config.addAllowedOrigin("http://localhost:4200");
    config.addAllowedHeader("*");
    config.setAllowedMethods(Arrays.asList("OPTIONS", "GET"));

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/actuator/**", config);

    return new CorsFilter(source);
}

CorsFilter - , . - , "//**". .

enter image description here

() , "Access-Control-Allow-Origin" . , (, @CrossOrigin - - MVC, CorsFilter).

/:

enter image description here

You can also create your own filter that adds a header to the http-response. I think it's better to use the standard approach, but in some cases debugging it may be useful to know that by adding a line to your filter, you can add what you want.

response.addHeader("Access-Control-Allow-Origin", "*");

Please note that the above does not contain the conditional check proposed earlier.

Hope this helps. Hurrah

0
source

Source: https://habr.com/ru/post/1015860/


All Articles