This commit is contained in:
root
2025-09-14 17:30:57 +05:00
parent c8512379d6
commit 4335189aa7
3 changed files with 19 additions and 112 deletions
@@ -2,10 +2,8 @@ package konturai.kz.api_gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "konturai.kz.api_gateway")
public class ApiGatewayApplication {
public static void main(String[] args) {
@@ -1,109 +0,0 @@
package konturai.kz.api_gateway.filter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class CorsHeaderFilter implements GlobalFilter, Ordered {
public CorsHeaderFilter() {
System.out.println("CorsHeaderFilter created and registered!");
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String path = request.getPath().value();
String method = request.getMethod().name();
// Log for debugging
System.out.println("CORS Filter - Path: " + path + ", Method: " + method);
// Handle preflight requests immediately
if (method.equals("OPTIONS")) {
System.out.println("Handling OPTIONS preflight request for: " + path);
ServerHttpResponse response = exchange.getResponse();
HttpHeaders headers = response.getHeaders();
// Clear any existing CORS headers
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN);
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS);
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS);
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS);
headers.remove(HttpHeaders.ACCESS_CONTROL_MAX_AGE);
// Set CORS headers for preflight
String origin = request.getHeaders().getFirst(HttpHeaders.ORIGIN);
System.out.println("Origin: " + origin);
if (origin != null && isAllowedOrigin(origin)) {
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
} else {
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "https://konturai.kz");
}
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS");
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*");
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
headers.set(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600");
System.out
.println("CORS headers set for preflight: " + headers.get(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
response.setStatusCode(org.springframework.http.HttpStatus.OK);
return Mono.empty();
}
// For non-preflight requests, process after the downstream service responds
System.out.println("Forwarding request to downstream service: " + path);
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
ServerHttpResponse response = exchange.getResponse();
HttpHeaders headers = response.getHeaders();
// Check if downstream service already set CORS headers
boolean hasCorsOrigin = headers.containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN);
System.out.println("After downstream service - Path: " + path + ", Has CORS: " + hasCorsOrigin);
// Clear any existing CORS headers from downstream service
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN);
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS);
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS);
headers.remove(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS);
headers.remove(HttpHeaders.ACCESS_CONTROL_MAX_AGE);
// Set our CORS headers
String origin = request.getHeaders().getFirst(HttpHeaders.ORIGIN);
if (origin != null && isAllowedOrigin(origin)) {
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
} else {
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "https://konturai.kz");
}
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS");
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*");
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
headers.set(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600");
System.out.println("CORS headers added for: " + path);
}));
}
private boolean isAllowedOrigin(String origin) {
return origin != null && (origin.equals("https://konturai.kz") ||
origin.startsWith("http://localhost:") ||
origin.startsWith("http://127.0.0.1:") ||
origin.startsWith("https://localhost:") ||
origin.startsWith("https://127.0.0.1:"));
}
@Override
public int getOrder() {
return -1; // High priority to run before other filters
}
}
+19 -1
View File
@@ -23,7 +23,25 @@ spring:
- Path=/** # <-- Матчится со всеми путями
order: 100 # <-- Низкий приоритет (сработает последним)
# CORS обрабатывается через CorsHeaderFilter
# CORS конфигурация
globalcors:
cors-configurations:
'[/**]':
allowed-origins:
- 'https://konturai.kz'
- 'http://localhost:*'
- 'http://127.0.0.1:*'
- 'https://localhost:*'
- 'https://127.0.0.1:*'
allowed-methods:
- GET
- POST
- PUT
- DELETE
- OPTIONS
allowed-headers: '*'
allow-credentials: true
max-age: 3600
# Логирование для отладки маршрутизации
logging: