From 74acfc208a3c8b5558fcf865c70234e89b1eee5b Mon Sep 17 00:00:00 2001 From: meowrain Date: Sat, 15 Nov 2025 23:42:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BD=91=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/encodings.xml | 2 + aioj-backend-gateway/pom.xml | 7 +- .../backend/gateway/config/CorsConfig.java | 68 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 aioj-backend-gateway/src/main/java/cn/meowrain/aioj/backend/gateway/config/CorsConfig.java diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 367cb5a..b0d0d5b 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,8 @@ + + diff --git a/aioj-backend-gateway/pom.xml b/aioj-backend-gateway/pom.xml index 7c32447..64f236c 100644 --- a/aioj-backend-gateway/pom.xml +++ b/aioj-backend-gateway/pom.xml @@ -16,5 +16,10 @@ 17 UTF-8 - + + + org.springframework.boot + spring-boot-starter-webflux + + \ No newline at end of file diff --git a/aioj-backend-gateway/src/main/java/cn/meowrain/aioj/backend/gateway/config/CorsConfig.java b/aioj-backend-gateway/src/main/java/cn/meowrain/aioj/backend/gateway/config/CorsConfig.java new file mode 100644 index 0000000..6d0c4c1 --- /dev/null +++ b/aioj-backend-gateway/src/main/java/cn/meowrain/aioj/backend/gateway/config/CorsConfig.java @@ -0,0 +1,68 @@ +package cn.meowrain.aioj.backend.gateway.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.reactive.CorsWebFilter; +import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; +import org.springframework.web.util.pattern.PathPatternParser; + +import java.util.List; + +/** + * 全局 CORS 配置(WebFlux 环境使用 CorsWebFilter) + * + * WebFlux 不使用 Spring MVC 的 CorsFilter, + * 而是使用专门的 CorsWebFilter 处理跨域。 + * + * 此配置实现了: + * - 允许任意域名访问(AllowedOriginPatterns = "*") + * - 允许所有请求方法(GET、POST、PUT...) + * - 允许所有请求头 + * - 允许跨域携带 Cookie(AllowCredentials) + * - 对所有路径生效(/**) + */ +@Configuration +public class CorsConfig { + + /** + * 注册全局 CORS 过滤器 + * + * @return CorsWebFilter 跨域过滤器 + */ + @Bean + public CorsWebFilter corsWebFilter() { + + // 创建跨域配置对象 + CorsConfiguration corsConfiguration = new CorsConfiguration(); + + // 允许所有请求方式:GET、POST、PUT、DELETE、OPTIONS... + corsConfiguration.addAllowedMethod("*"); + + // 是否允许携带 Cookie 信息。跨域默认不允许,需要显式开启 + corsConfiguration.setAllowCredentials(true); + + /** + * 允许跨域的来源域名 + * 使用 setAllowedOriginPatterns("*") 是 WebFlux 推荐方式, + * 因为 setAllowedOrigins("*") 在 allowCredentials=true 时会被拦截。 + */ + corsConfiguration.setAllowedOriginPatterns(List.of("*")); + + // 允许所有请求头,例如 Authorization、Content-Type 等 + corsConfiguration.addAllowedHeader("*"); + + /** + * 基于 URL 的跨域配置源, + * PathPatternParser 用于解析路径模式(更高性能) + */ + UrlBasedCorsConfigurationSource source = + new UrlBasedCorsConfigurationSource(new PathPatternParser()); + + // 对所有路径应用跨域设置 + source.registerCorsConfiguration("/**", corsConfiguration); + + // 创建并返回 WebFlux 专用的 CORS 过滤器 + return new CorsWebFilter(source); + } +}