spring-bootcors

Spring Boot enabling CORS by application.properties


I using Spring Boot API RESTful that get up automatically by your Entities Class. I'm consuming this apiRest from a front-end web app but it gives me this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource

I'm setting the CORS configuration using the applicantion.properties specified here.

My basic configuration is:

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

I have tried different combinations in those variables but still not working. Any ideas?


Solution

  • I got the answer by myself:

    Just add this to application.java

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