From 0920a83e1ce28f6457ecdb088aa0caff056fb0fe Mon Sep 17 00:00:00 2001 From: gewuyou Date: Fri, 9 May 2025 17:40:47 +0800 Subject: [PATCH] feat(version): Cross-domain configuration is supported for API version request mappings - Inject VersionProperties and CorsConfigurationSource in VersionAutoConfiguration - Updated the apiVersionRequestMappingHandlerMapping method to add support for cross-origin configuration - Remove unnecessary comments from R classes --- .../com/gewuyou/forgeboot/common/result/R.kt | 4 ++-- .../version/config/VersionAutoConfiguration.kt | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/forgeboot-common/forgeboot-common-result/forgeboot-common-result-impl/src/main/kotlin/com/gewuyou/forgeboot/common/result/R.kt b/forgeboot-common/forgeboot-common-result/forgeboot-common-result-impl/src/main/kotlin/com/gewuyou/forgeboot/common/result/R.kt index d663369..16aa130 100644 --- a/forgeboot-common/forgeboot-common-result/forgeboot-common-result-impl/src/main/kotlin/com/gewuyou/forgeboot/common/result/R.kt +++ b/forgeboot-common/forgeboot-common-result/forgeboot-common-result-impl/src/main/kotlin/com/gewuyou/forgeboot/common/result/R.kt @@ -18,7 +18,7 @@ data class R( val message: String, val data: T? = null, val requestId: String? = null, - val extra: Map = emptyMap() // ✅ 扩展字段保存位置 + val extra: Map = emptyMap() ) { /** * 转换为可变 Map,包含 extra 中的字段 @@ -33,7 +33,7 @@ data class R( if (!requestId.isNullOrBlank()) { map["requestId"] = requestId } - map.putAll(extra) // ✅ 扁平化合并 + map.putAll(extra) return map } diff --git a/forgeboot-webmvc/forgeboot-webmvc-version-starter/src/main/kotlin/com/gewuyou/forgeboot/webmvc/version/config/VersionAutoConfiguration.kt b/forgeboot-webmvc/forgeboot-webmvc-version-starter/src/main/kotlin/com/gewuyou/forgeboot/webmvc/version/config/VersionAutoConfiguration.kt index 93ac99c..be5b7a3 100644 --- a/forgeboot-webmvc/forgeboot-webmvc-version-starter/src/main/kotlin/com/gewuyou/forgeboot/webmvc/version/config/VersionAutoConfiguration.kt +++ b/forgeboot-webmvc/forgeboot-webmvc-version-starter/src/main/kotlin/com/gewuyou/forgeboot/webmvc/version/config/VersionAutoConfiguration.kt @@ -6,6 +6,8 @@ import com.gewuyou.forgeboot.webmvc.version.mapping.ApiVersionRequestMappingHand import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.web.cors.CorsConfigurationSource +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer /** *版本自动配置 @@ -15,7 +17,10 @@ import org.springframework.context.annotation.Configuration */ @Configuration @EnableConfigurationProperties(VersionProperties::class) -open class VersionAutoConfiguration { +open class VersionAutoConfiguration( + private val versionProperties: VersionProperties, + private val corsConfigurationSource: CorsConfigurationSource +) : WebMvcConfigurer { /** * 创建并配置一个 ApiVersionRequestMappingHandlerMapping 实例 * @@ -26,8 +31,11 @@ open class VersionAutoConfiguration { * @return ApiVersionRequestMappingHandlerMapping 实例,用于处理基于 API 版本的请求映射 */ @Bean - open fun apiVersionRequestMappingHandlerMapping(versionProperties: VersionProperties): ApiVersionRequestMappingHandlerMapping { + open fun apiVersionRequestMappingHandlerMapping(): ApiVersionRequestMappingHandlerMapping { log.info("创建 API 版本请求映射处理程序映射") - return ApiVersionRequestMappingHandlerMapping(versionProperties).also { it.order = Int.MIN_VALUE } + return ApiVersionRequestMappingHandlerMapping(versionProperties).also { + it.order = Int.MIN_VALUE + it.corsConfigurationSource = corsConfigurationSource + } } } \ No newline at end of file