fix(backend): fixed cors error on build dev environment
This commit is contained in:
parent
b856d5d105
commit
78c3292dd9
1 changed files with 14 additions and 2 deletions
|
|
@ -32,6 +32,8 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
|
|
@ -93,13 +95,23 @@ public class SecurityConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public CorsConfigurationSource corsConfigurationSource() {
|
public CorsConfigurationSource corsConfigurationSource() {
|
||||||
CorsConfiguration configuration = new CorsConfiguration();
|
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.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"));
|
||||||
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "X-Access-Token"));
|
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "X-Access-Token"));
|
||||||
configuration.setAllowCredentials(true);
|
configuration.setAllowCredentials(true);
|
||||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
source.registerCorsConfiguration("/**", configuration);
|
source.registerCorsConfiguration("/**", configuration);
|
||||||
|
System.out.println("LOGGED CORS ORIGINS: " + corsOrigins);
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue