refactor(llmx-core):调整跨域配置并优化部分代码 #20
| @ -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() | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -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)  // 是否允许携带Cookie(true时需要明确指定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)  // 是否允许携带Cookie(true时需要明确指定allowedOrigins,不能为*) | ||||||
|  | //            .maxAge(3600)  // 预检请求缓存时间(秒) | ||||||
|  | //    } | ||||||
|  | //} | ||||||
|  | |||||||
| @ -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 | ||||||
| ) { | ) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user