Search code examples
javaspringspring-mvccorsspring-boot-actuator

Spring CORS settings


I'm trying to enable the cross origin header to be able to reach the service from anywhere (only on local env) but I cannot.

@Configuration
@RequiredArgsConstructor
public class CrossOriginConfig implements WebMvcConfigurer {
    private final SecurityConfiguration securityConfiguration;

    @Override
    public void addCorsMappings(CorsRegistry registry) { 
    registry.addMapping("*").allowedOrigins(securityConfiguration.getCrossOrginFilter());
    }
}

I made a custom index.html with an ajax call and it fails due to the Allow-Cross-Origin header missing and it comes from another origin.

Simple Spring Boot 2.0 controllers are used with @RestController annotation and simple @GetMapping.

What I missed? What should I include and where?


Solution

  • As Georg Wittberger pointed out the problem was with the mapping. I used the * wildcard what is not good for paths.

    Instead of this: registry.addMapping("*")

    I used this: registry.addMapping("/**") and it's working fine.