From 7400c4d24f8ab09885342fe1abd0ee41cb33a481 Mon Sep 17 00:00:00 2001 From: gewuyou Date: Fri, 9 May 2025 15:12:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(llmx-core):=E8=B0=83=E6=95=B4=E8=B7=A8?= =?UTF-8?q?=E5=9F=9F=E9=85=8D=E7=BD=AE=E5=B9=B6=E4=BC=98=E5=8C=96=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除全局 CorsConfig 类,注释保留备用- 在 ChatController 中添加跨域配置 - 将 AppConfiguration 类和 webClientBuilder 方法改为开放 --- .../core/service/config/AppConfiguration.kt | 4 +- .../llmx/core/service/config/CorsConfig.kt | 59 +++++++++++-------- .../core/service/controller/ChatController.kt | 2 + 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/AppConfiguration.kt b/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/AppConfiguration.kt index fda061a..6d400c3 100644 --- a/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/AppConfiguration.kt +++ b/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/AppConfiguration.kt @@ -14,7 +14,7 @@ import org.springframework.web.reactive.function.client.WebClient */ @Configuration @EnableConfigurationProperties(ModelProperties::class) -class AppConfiguration { +open class AppConfiguration { /** * 创建一个配置了负载均衡的WebClient构建器 * @@ -25,7 +25,7 @@ class AppConfiguration { */ @Bean @LoadBalanced - fun webClientBuilder(): WebClient.Builder { + open fun webClientBuilder(): WebClient.Builder { return WebClient.builder() } } \ No newline at end of file diff --git a/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/CorsConfig.kt b/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/CorsConfig.kt index e23a21a..e955bf5 100644 --- a/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/CorsConfig.kt +++ b/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/config/CorsConfig.kt @@ -1,23 +1,36 @@ -package org.jcnc.llmx.core.service.config - -import org.springframework.context.annotation.Configuration -import org.springframework.web.servlet.config.annotation.CorsRegistry -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer - -/** - *CORS配置 - * - * @since 2025-04-02 17:03:41 - * @author gewuyou - */ -@Configuration -open class CorsConfig : WebMvcConfigurer { - override fun addCorsMappings(registry: CorsRegistry) { - registry.addMapping("/**") // 匹配所有路径 - .allowedOrigins("*") // 允许所有来源(生产环境建议指定具体域名) - .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法 - .allowedHeaders("*") // 允许所有请求头 - .allowCredentials(false) // 是否允许携带Cookie(true时需要明确指定allowedOrigins,不能为*) - .maxAge(3600) // 预检请求缓存时间(秒) - } -} \ No newline at end of file +//package org.jcnc.llmx.core.service.config +// +//import com.gewuyou.forgeboot.core.extension.log +//import org.springframework.context.annotation.Configuration +//import org.springframework.web.servlet.config.annotation.CorsRegistry +//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer +// +///** +// * CORS配置类 +// * +// * 该类用于全局配置跨域请求设置,以确保前端应用可以与后端服务进行通信 +// * 主要通过重写addCorsMappings方法来配置跨域请求映射,以及通过corsWebFilter方法来提供更细粒度的跨域支持 +// * +// * @since 2025-04-02 17:03:41 +// * @author gewuyou +// */ +//@Configuration +//open class CorsConfig : WebMvcConfigurer { +// /** +// * 添加跨域请求映射 +// * +// * 该方法重写了父接口中的addCorsMappings方法,用于配置全局的跨域请求规则 +// * 主要配置了允许所有路径、所有来源、常见HTTP方法、所有请求头的跨域请求,并设置了不携带Cookie以及预检请求缓存时间 +// * +// * @param registry 跨域请求注册表,用于添加跨域请求映射 +// */ +// override fun addCorsMappings(registry: CorsRegistry) { +// log.info("Web CORS配置生效") +// registry.addMapping("/**") // 匹配所有路径 +// .allowedOrigins("*") // 允许所有来源(生产环境建议指定具体域名) +// .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法 +// .allowedHeaders("*") // 允许所有请求头 +// .allowCredentials(false) // 是否允许携带Cookie(true时需要明确指定allowedOrigins,不能为*) +// .maxAge(3600) // 预检请求缓存时间(秒) +// } +//} diff --git a/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/controller/ChatController.kt b/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/controller/ChatController.kt index e905147..dd8d64b 100644 --- a/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/controller/ChatController.kt +++ b/llmx-core/llmx-core-service/src/main/kotlin/org/jcnc/llmx/core/service/controller/ChatController.kt @@ -8,6 +8,7 @@ import org.jcnc.llmx.core.service.service.impl.LLMServiceImpl import org.reactivestreams.Publisher import org.springframework.http.MediaType +import org.springframework.web.bind.annotation.CrossOrigin import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody @@ -23,6 +24,7 @@ import org.springframework.web.bind.annotation.RestController @ApiVersion @RestController @RequestMapping("/chat") +@CrossOrigin(origins = ["*"]) // 放宽跨域 class ChatController( private val llmServiceImpl: LLMServiceImpl ) {