Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
/// Type-safe CORS configuration properties bound from `spring.web.cors`.
@ConfigurationProperties(prefix = "spring.web.cors")
public record CorsProperties(
List<String> allowedOrigins
List<String> allowedOrigins,
List<String> allowedOriginPatterns
) {
public CorsProperties {
if (allowedOriginPatterns == null) {
allowedOriginPatterns = List.of();
}
if (allowedOrigins == null) {
allowedOrigins = List.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(corsProperties.allowedOrigins());

// Exact origins (no wildcard, safe with allowCredentials)
if (!corsProperties.allowedOrigins().isEmpty()) {
configuration.setAllowedOrigins(corsProperties.allowedOrigins());
}

// Pattern-based origins (supports wildcards, e.g. https://*.decathlon.io)
if (!corsProperties.allowedOriginPatterns().isEmpty()) {
configuration.setAllowedOriginPatterns(corsProperties.allowedOriginPatterns());
}

configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(List.of("*"));
configuration.setAllowCredentials(true);
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ spring:
# Disables serving of static resources (HTML, JS, CSS, images).
add-mappings: false
cors:
# URLs allowed to make cross-origin API calls, set via SPRING_WEB_CORS_ALLOWED_ORIGINS env var (comma-separated list).
# Exact origins — set via SPRING_WEB_CORS_ALLOWED_ORIGINS env var (comma-separated).
# Use for known, fixed origins.
allowed-origins: ${SPRING_WEB_CORS_ALLOWED_ORIGINS:}
# Pattern-based origins — set via SPRING_WEB_CORS_ALLOWED_ORIGIN_PATTERNS env var (comma-separated).
# Supports wildcards, e.g. https://*.decathlon.io. Compatible with allowCredentials(true).
allowed-origin-patterns: ${SPRING_WEB_CORS_ALLOWED_ORIGIN_PATTERNS:}

jackson:
# Serializes all JSON fields as snake_case (for example templateIdentifier → template_identifier).
Expand Down
Loading