refactor(llmx-core):调整跨域配置并优化部分代码
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 4m13s

- 移除全局 CorsConfig 类,注释保留备用- 在 ChatController 中添加跨域配置
- 将 AppConfiguration 类和 webClientBuilder 方法改为开放
This commit is contained in:
gewuyou 2025-05-09 15:12:31 +08:00
parent e6149ecb02
commit 7400c4d24f
3 changed files with 40 additions and 25 deletions

View File

@ -14,7 +14,7 @@ import org.springframework.web.reactive.function.client.WebClient
*/ */
@Configuration @Configuration
@EnableConfigurationProperties(ModelProperties::class) @EnableConfigurationProperties(ModelProperties::class)
class AppConfiguration { open class AppConfiguration {
/** /**
* 创建一个配置了负载均衡的WebClient构建器 * 创建一个配置了负载均衡的WebClient构建器
* *
@ -25,7 +25,7 @@ class AppConfiguration {
*/ */
@Bean @Bean
@LoadBalanced @LoadBalanced
fun webClientBuilder(): WebClient.Builder { open fun webClientBuilder(): WebClient.Builder {
return WebClient.builder() return WebClient.builder()
} }
} }

View File

@ -1,23 +1,36 @@
package org.jcnc.llmx.core.service.config //package org.jcnc.llmx.core.service.config
//
import org.springframework.context.annotation.Configuration //import com.gewuyou.forgeboot.core.extension.log
import org.springframework.web.servlet.config.annotation.CorsRegistry //import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer //import org.springframework.web.servlet.config.annotation.CorsRegistry
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
/** //
*CORS配置 ///**
* // * CORS配置类
* @since 2025-04-02 17:03:41 // *
* @author gewuyou // * 该类用于全局配置跨域请求设置,以确保前端应用可以与后端服务进行通信
*/ // * 主要通过重写addCorsMappings方法来配置跨域请求映射以及通过corsWebFilter方法来提供更细粒度的跨域支持
@Configuration // *
open class CorsConfig : WebMvcConfigurer { // * @since 2025-04-02 17:03:41
override fun addCorsMappings(registry: CorsRegistry) { // * @author gewuyou
registry.addMapping("/**") // 匹配所有路径 // */
.allowedOrigins("*") // 允许所有来源(生产环境建议指定具体域名) //@Configuration
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法 //open class CorsConfig : WebMvcConfigurer {
.allowedHeaders("*") // 允许所有请求头 // /**
.allowCredentials(false) // 是否允许携带Cookietrue时需要明确指定allowedOrigins不能为* // * 添加跨域请求映射
.maxAge(3600) // 预检请求缓存时间(秒) // *
} // * 该方法重写了父接口中的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) // 是否允许携带Cookietrue时需要明确指定allowedOrigins不能为*
// .maxAge(3600) // 预检请求缓存时间(秒)
// }
//}

View File

@ -8,6 +8,7 @@ import org.jcnc.llmx.core.service.service.impl.LLMServiceImpl
import org.reactivestreams.Publisher import org.reactivestreams.Publisher
import org.springframework.http.MediaType 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.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
@ -23,6 +24,7 @@ import org.springframework.web.bind.annotation.RestController
@ApiVersion @ApiVersion
@RestController @RestController
@RequestMapping("/chat") @RequestMapping("/chat")
@CrossOrigin(origins = ["*"]) // 放宽跨域
class ChatController( class ChatController(
private val llmServiceImpl: LLMServiceImpl private val llmServiceImpl: LLMServiceImpl
) { ) {