This commit is contained in:
Mykhailo Svishchov
2026-03-05 14:30:54 +02:00
parent 2177bc7baa
commit db35af8912

View File

@@ -6,26 +6,42 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Configuration @Configuration
public class CorsConfig { public class CorsConfig {
@Value("${FRONTEND_URL:https://example.com}") @Value("${FRONTEND_URL:}")
private String frontendUrl; private String frontendUrl;
private static final List<String> ADDITIONAL_ORIGINS = Arrays.asList(
"https://honey-test-fe-production.up.railway.app",
"https://web.telegram.org",
"https://webk.telegram.org",
"https://t.me"
);
@Bean @Bean
public WebMvcConfigurer corsConfigurer() { public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() { return new WebMvcConfigurer() {
@Override @Override
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
List<String> origins = Stream.concat(
Arrays.stream(frontendUrl != null && !frontendUrl.isBlank()
? frontendUrl.split("\\s*,\\s*")
: new String[]{}),
ADDITIONAL_ORIGINS.stream()
).filter(s -> s != null && !s.isBlank()).distinct().collect(Collectors.toList());
if (origins.isEmpty()) {
origins = ADDITIONAL_ORIGINS;
}
registry.addMapping("/**") registry.addMapping("/**")
.allowedOrigins( .allowedOrigins(origins.toArray(new String[0]))
frontendUrl,
"https://web.telegram.org",
"https://webk.telegram.org",
"https://t.me",
"https://*.t.me"
)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*") .allowedHeaders("*")
.allowCredentials(true) .allowCredentials(true)