From 9d3795f34fe3bf39f09ed2de572878c2b87591c9 Mon Sep 17 00:00:00 2001 From: litianxiang Date: Tue, 5 May 2026 16:41:09 +0800 Subject: [PATCH] =?UTF-8?q?cors=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/aida/gateway/config/CorsConfig.java | 73 +++---------------- 1 file changed, 10 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/aida/gateway/config/CorsConfig.java b/src/main/java/com/aida/gateway/config/CorsConfig.java index a0d6213..bae4918 100644 --- a/src/main/java/com/aida/gateway/config/CorsConfig.java +++ b/src/main/java/com/aida/gateway/config/CorsConfig.java @@ -1,71 +1,18 @@ package com.aida.gateway.config; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.server.reactive.ServerHttpRequest; -import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.stereotype.Component; -import org.springframework.web.cors.reactive.CorsUtils; -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.WebFilter; -import org.springframework.web.server.WebFilterChain; -import reactor.core.publisher.Mono; - -import java.util.Arrays; -import java.util.List; +import org.springframework.web.reactive.config.CorsRegistry; +import org.springframework.web.reactive.config.WebFluxConfigurer; @Configuration -public class CorsConfig { +public class CorsConfig implements WebFluxConfigurer { - private static final List ALLOWED_HEADERS = Arrays.asList( - "Origin", "Content-Type", "Accept", "Authorization", - "X-Requested-With", "Access-Control-Request-Method", - "Access-Control-Request-Headers" - ); - - private static final List ALLOWED_METHODS = Arrays.asList( - HttpMethod.GET.name(), HttpMethod.POST.name(), - HttpMethod.PUT.name(), HttpMethod.DELETE.name(), - HttpMethod.OPTIONS.name(), HttpMethod.PATCH.name() - ); - - private static final long MAX_AGE = 3600L; - - @Bean - @Order(Ordered.HIGHEST_PRECEDENCE) - public WebFilter corsWebFilter() { - return (ServerWebExchange exchange, WebFilterChain chain) -> { - ServerHttpRequest request = exchange.getRequest(); - - if (!CorsUtils.isCorsRequest(request)) { - return chain.filter(exchange); - } - - ServerHttpResponse response = exchange.getResponse(); - HttpHeaders headers = response.getHeaders(); - - String origin = request.getHeaders().getOrigin(); - if (origin == null || origin.isEmpty()) { - origin = "*"; - } - - headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin); - headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, String.join(",", ALLOWED_METHODS)); - headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, String.join(",", ALLOWED_HEADERS)); - headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, String.join(",", ALLOWED_HEADERS)); - headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, String.valueOf(MAX_AGE)); - - if (CorsUtils.isPreFlightRequest(request)) { - response.setStatusCode(HttpStatus.OK); - return Mono.empty(); - } - - return chain.filter(exchange); - }; + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") + .allowCredentials(true) + .allowedMethods("GET", "POST", "PUT", "DELETE") + .maxAge(3600); } }