endpoints.cors.allowed-origins
for Spring boot 1 or management.endpoints.web.cors.allowed-origins
for Spring boot 2 are used to apply CORS to the Actuator endpoints, it does not apply to controller endpoints you defined.
Actually, by default Spring boot doesn't set any CORS headers. If you're seeing Access-Control-Allow-Origin
with a value (eg. a wildcard), it means that you're configuring that somewhere within your own code. Verify if you're using @CrossOrigin
on your controllers, or that you're having some sort of Filter
(eg. CorsFilter
).
One way to configure CORS globally within Spring boot is by defining a CorsFilter
bean, for example:
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(List.of("https://example.com"));
config.setAllowedHeaders(List.of("Origin", "Content-Type", "Accept"));
config.setAllowedMethods(List.of("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
For other possibilities, you can check this question. Be aware, this will only work properly if you find out what is causing the Access-Control-Allow-Origin
to be set.