fix(backend): fixed cors error on build dev environment

This commit is contained in:
Bernhard Müller 2026-07-28 13:19:40 +02:00
parent b856d5d105
commit 78c3292dd9

View file

@ -32,6 +32,8 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@Configuration
@EnableWebSecurity
@ -93,13 +95,23 @@ public class SecurityConfig {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOriginPatterns(Arrays.stream(corsOrigins.split(",")).map(String::trim).toList());
List<String> allowedPatterns = Arrays.stream(corsOrigins.split(","))
.map(String::trim)
.flatMap(origin -> {
if (origin.contains("*")) {
return Stream.of(origin, origin + ":*");
}
return Stream.of(origin);
})
.toList();
configuration.setAllowedOriginPatterns(allowedPatterns);
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"));
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "X-Access-Token"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
System.out.println("LOGGED CORS ORIGINS: " + corsOrigins);
return source;
}