From 267c9ca704c7afceb1fc165465b827c8e0fa095a Mon Sep 17 00:00:00 2001 From: Hccake Date: Wed, 31 Jan 2024 22:43:30 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20CORS=20=E9=85=8D=E7=BD=AE=E4=BB=8E=20Op?= =?UTF-8?q?enAPI=20starter=20=E8=BF=81=E7=A7=BB=E5=88=B0=20Web=20stater=20?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openapi/OpenApiAutoConfiguration.java | 36 ---------- .../openapi/OpenApiProperties.java | 66 ----------------- .../web/servlet/WebMvcAutoConfiguration.java | 42 +++++++++++ .../web/servlet/WebProperties.java | 72 ++++++++++++++++++- 4 files changed, 113 insertions(+), 103 deletions(-) diff --git a/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiAutoConfiguration.java b/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiAutoConfiguration.java index 67921ba40..5c2977cf3 100644 --- a/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiAutoConfiguration.java +++ b/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiAutoConfiguration.java @@ -37,12 +37,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; -import org.springframework.core.Ordered; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import org.springframework.web.filter.CorsFilter; /** * OpenAPI 的自动配置类 @@ -96,37 +91,6 @@ private Info convertInfo(OpenApiProperties.InfoProperties infoProperties) { return info; } - /** - * 允许聚合者对提供者的文档进行跨域访问 解决聚合文档导致的跨域问题 - * @return FilterRegistrationBean - */ - @Bean - @ConditionalOnProperty(prefix = OpenApiProperties.PREFIX + ".cors-config", name = "enabled", havingValue = "true") - public FilterRegistrationBean corsFilterRegistrationBean() { - // 获取 CORS 配置 - OpenApiProperties.CorsConfig corsConfig = this.openApiProperties.getCorsConfig(); - - // 转换 CORS 配置 - CorsConfiguration corsConfiguration = new CorsConfiguration(); - corsConfiguration.setAllowedOrigins(corsConfig.getAllowedOrigins()); - corsConfiguration.setAllowedOriginPatterns(corsConfig.getAllowedOriginPatterns()); - corsConfiguration.setAllowedMethods(corsConfig.getAllowedMethods()); - corsConfiguration.setAllowedHeaders(corsConfig.getAllowedHeaders()); - corsConfiguration.setExposedHeaders(corsConfig.getExposedHeaders()); - corsConfiguration.setAllowCredentials(corsConfig.getAllowCredentials()); - corsConfiguration.setMaxAge(corsConfig.getMaxAge()); - - // 注册 CORS 配置与资源的映射关系 - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration(corsConfig.getUrlPattern(), corsConfiguration); - - // 注册 CORS 过滤器,设置最高优先级 - FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source)); - bean.setOrder(Ordered.HIGHEST_PRECEDENCE); - - return bean; - } - /** * The type Spring doc pageParam configuration. */ diff --git a/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiProperties.java b/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiProperties.java index 82460a385..d26e1315a 100644 --- a/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiProperties.java +++ b/openapi/ballcat-spring-boot-starter-openapi/src/main/java/org/ballcat/autoconfigure/openapi/OpenApiProperties.java @@ -16,8 +16,6 @@ package org.ballcat.autoconfigure.openapi; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -32,7 +30,6 @@ import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; -import org.springframework.web.cors.CorsConfiguration; /** * @author Hccake 2019/11/1 19:37 @@ -98,11 +95,6 @@ public class OpenApiProperties { */ private Map extensions = null; - /** - * 跨域配置 - */ - private CorsConfig corsConfig; - /** *

* 文档的基础属性信息 @@ -154,62 +146,4 @@ public static class InfoProperties { } - /** - *

- * 跨域配置,用于文档聚合. - *

- * - * @see CorsConfiguration - */ - @Data - public static class CorsConfig { - - /** - * 开启 Cors 跨域配置 - */ - private boolean enabled = false; - - /** - * 跨域对应的 url 匹配规则 - */ - private String urlPattern = "/**"; - - /** - * 允许跨域的源 - */ - private List allowedOrigins; - - /** - * 允许跨域来源的匹配规则 - */ - private List allowedOriginPatterns; - - /** - * 允许跨域的方法列表 - */ - private List allowedMethods = new ArrayList<>(Collections.singletonList(CorsConfiguration.ALL)); - - /** - * 允许跨域的头信息 - */ - private List allowedHeaders = new ArrayList<>(Collections.singletonList(CorsConfiguration.ALL)); - - /** - * 额外允许跨域请求方获取的 response header 信息 - */ - private List exposedHeaders = new ArrayList<>(Collections.singletonList("traceId")); - - /** - * 是否允许跨域发送 Cookie - */ - private Boolean allowCredentials = true; - - /** - * CORS 配置缓存时间,用于控制浏览器端是否发起 Option 预检请求。 若配置此参数,在第一次获取到 CORS - * 的配置信息后,在过期时间内,浏览器将直接发出请求,跳过 option 预检 - */ - private Long maxAge; - - } - } diff --git a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index 59b95f013..cc4b167d3 100644 --- a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -27,11 +27,15 @@ import org.bson.types.ObjectId; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -43,6 +47,8 @@ @EnableConfigurationProperties({ WebProperties.class, PageableProperties.class }) public class WebMvcAutoConfiguration { + private final WebProperties webProperties; + @Bean @ConditionalOnMissingBean public PageParamArgumentResolver pageParamArgumentResolver(PageableProperties pageableProperties) { @@ -51,6 +57,42 @@ public PageParamArgumentResolver pageParamArgumentResolver(PageableProperties pa pageableProperties.getSortParameterName()); } + /** + * 允许聚合者对提供者的文档进行跨域访问 解决聚合文档导致的跨域问题 + * @return FilterRegistrationBean + */ + @Bean + @ConditionalOnProperty(prefix = WebProperties.PREFIX + ".cors-config", name = "enabled", havingValue = "true") + public FilterRegistrationBean corsFilterRegistrationBean() { + // 获取 CORS 配置 + WebProperties.CorsConfig corsConfig = this.webProperties.getCorsConfig(); + + // 转换 CORS 配置 + CorsConfiguration corsConfiguration = getCorsConfiguration(corsConfig); + + // 注册 CORS 配置与资源的映射关系 + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration(corsConfig.getUrlPattern(), corsConfiguration); + + // 注册 CORS 过滤器,设置最高优先级 + 1 (在 traceId 之后) + FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source)); + bean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1000); + + return bean; + } + + private static CorsConfiguration getCorsConfiguration(WebProperties.CorsConfig corsConfig) { + CorsConfiguration corsConfiguration = new CorsConfiguration(); + corsConfiguration.setAllowedOrigins(corsConfig.getAllowedOrigins()); + corsConfiguration.setAllowedOriginPatterns(corsConfig.getAllowedOriginPatterns()); + corsConfiguration.setAllowedMethods(corsConfig.getAllowedMethods()); + corsConfiguration.setAllowedHeaders(corsConfig.getAllowedHeaders()); + corsConfiguration.setExposedHeaders(corsConfig.getExposedHeaders()); + corsConfiguration.setAllowCredentials(corsConfig.getAllowCredentials()); + corsConfiguration.setMaxAge(corsConfig.getMaxAge()); + return corsConfiguration; + } + @RequiredArgsConstructor @Configuration(proxyBeanMethods = false) static class CustomWebMvcConfigurer implements WebMvcConfigurer { diff --git a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebProperties.java b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebProperties.java index d98b1a64e..83ed63c91 100644 --- a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebProperties.java +++ b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/servlet/WebProperties.java @@ -16,19 +16,89 @@ package org.ballcat.autoconfigure.web.servlet; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.web.cors.CorsConfiguration; /** * @author hccake */ @Data -@ConfigurationProperties(prefix = "ballcat.web") +@ConfigurationProperties(prefix = WebProperties.PREFIX) public class WebProperties { + public static final String PREFIX = "ballcat.web"; + /** * traceId 的 http 头名称 */ private String traceIdHeaderName = "X-Trace-Id"; + /** + * 跨域配置 + */ + private CorsConfig corsConfig; + + /** + *

+ * 跨域配置. + *

+ * + * @see CorsConfiguration + */ + @Data + public static class CorsConfig { + + /** + * 开启 Cors 跨域配置 + */ + private boolean enabled = false; + + /** + * 跨域对应的 url 匹配规则 + */ + private String urlPattern = "/**"; + + /** + * 允许跨域的源 + */ + private List allowedOrigins; + + /** + * 允许跨域来源的匹配规则 + */ + private List allowedOriginPatterns; + + /** + * 允许跨域的方法列表 + */ + private List allowedMethods = new ArrayList<>(Collections.singletonList(CorsConfiguration.ALL)); + + /** + * 允许跨域的头信息 + */ + private List allowedHeaders = new ArrayList<>(Collections.singletonList(CorsConfiguration.ALL)); + + /** + * 额外允许跨域请求方获取的 response header 信息 + */ + private List exposedHeaders = new ArrayList<>(Collections.singletonList("traceId")); + + /** + * 是否允许跨域发送 Cookie + */ + private Boolean allowCredentials = true; + + /** + * CORS 配置缓存时间,用于控制浏览器端是否发起 Option 预检请求。 若配置此参数,在第一次获取到 CORS + * 的配置信息后,在过期时间内,浏览器将直接发出请求,跳过 option 预检 + */ + private Long maxAge; + + } + }