This commit is contained in:
2025-08-28 17:09:21 +08:00
parent f324f15397
commit ecc7495954
6 changed files with 48 additions and 10 deletions

View File

@@ -228,7 +228,7 @@ development:
debug: true debug: true
enable_profiler: true enable_profiler: true
enable_cors: true enable_cors: true
cors_allowed_origins: "https://consoletest.tianyuanapi.com,https://console.tianyuanapi.com" cors_allowed_origins: "http://localhost:5173,https://consoletest.tianyuanapi.com,https://console.tianyuanapi.com"
cors_allowed_methods: "GET,POST,PUT,PATCH,DELETE,OPTIONS" cors_allowed_methods: "GET,POST,PUT,PATCH,DELETE,OPTIONS"
cors_allowed_headers: "Origin,Content-Type,Accept,Authorization,X-Requested-With,Access-Id" cors_allowed_headers: "Origin,Content-Type,Accept,Authorization,X-Requested-With,Access-Id"

View File

@@ -105,4 +105,9 @@ zhicha:
url: "http://proxy.tianyuanapi.com/dataMiddle/api/handle" url: "http://proxy.tianyuanapi.com/dataMiddle/api/handle"
app_id: "4b78fff61ab8426f" app_id: "4b78fff61ab8426f"
app_secret: "1128f01b94124ae899c2e9f2b1f37681" app_secret: "1128f01b94124ae899c2e9f2b1f37681"
encrypt_key: "af4ca0098e6a202a5c08c413ebd9fd62" encrypt_key: "af4ca0098e6a202a5c08c413ebd9fd62"
development:
enable_cors: true
cors_allowed_origins: "http://localhost:5173,http://localhost:8080"
cors_allowed_methods: "GET,POST,PUT,PATCH,DELETE,OPTIONS"
cors_allowed_headers: "Origin,Content-Type,Accept,Authorization,X-Requested-With,Access-Id"

View File

@@ -18,7 +18,7 @@ server:
# =========================================== # ===========================================
development: development:
enable_cors: true enable_cors: true
cors_allowed_origins: "https://consoletest.tianyuanapi.com,https://console.tianyuanapi.com" cors_allowed_origins: "http://localhost:5173,https://consoletest.tianyuanapi.com,https://console.tianyuanapi.com"
cors_allowed_methods: "GET,POST,PUT,PATCH,DELETE,OPTIONS" cors_allowed_methods: "GET,POST,PUT,PATCH,DELETE,OPTIONS"
cors_allowed_headers: "Origin,Content-Type,Accept,Authorization,X-Requested-With,Access-Id" cors_allowed_headers: "Origin,Content-Type,Accept,Authorization,X-Requested-With,Access-Id"

View File

@@ -40,6 +40,9 @@ func (r *ApiRoutes) Register(router *sharedhttp.GinRouter) {
{ {
apiGroup.POST("/:api_name", r.domainAuthMiddleware.Handle(""), r.apiHandler.HandleApiCall) apiGroup.POST("/:api_name", r.domainAuthMiddleware.Handle(""), r.apiHandler.HandleApiCall)
// Console专用接口 - 使用JWT认证不需要域名认证
apiGroup.POST("/console/:api_name", r.authMiddleware.Handle(), r.apiHandler.HandleApiCall)
// 表单配置接口(用于前端动态生成表单) // 表单配置接口(用于前端动态生成表单)
apiGroup.GET("/form-config/:api_code", r.authMiddleware.Handle(), r.apiHandler.GetFormConfig) apiGroup.GET("/form-config/:api_code", r.authMiddleware.Handle(), r.apiHandler.GetFormConfig)

View File

@@ -134,9 +134,18 @@ func (r *GinRouter) GetEngine() *gin.Engine {
// applyMiddlewares 应用中间件 // applyMiddlewares 应用中间件
func (r *GinRouter) applyMiddlewares() { func (r *GinRouter) applyMiddlewares() {
// 按优先级排序中间件 // 按优先级排序中间件,优先级相同时按名称排序确保稳定性
sort.Slice(r.middlewares, func(i, j int) bool { sort.Slice(r.middlewares, func(i, j int) bool {
return r.middlewares[i].GetPriority() > r.middlewares[j].GetPriority() priorityI := r.middlewares[i].GetPriority()
priorityJ := r.middlewares[j].GetPriority()
// 如果优先级不同,按优先级降序排列
if priorityI != priorityJ {
return priorityI > priorityJ
}
// 如果优先级相同,按名称排序确保稳定性
return r.middlewares[i].GetName() < r.middlewares[j].GetName()
}) })
// 应用全局中间件 // 应用全局中间件

View File

@@ -27,7 +27,7 @@ func (m *CORSMiddleware) GetName() string {
// GetPriority 返回中间件优先级 // GetPriority 返回中间件优先级
func (m *CORSMiddleware) GetPriority() int { func (m *CORSMiddleware) GetPriority() int {
return 100 // 高优先级,最先执行 return 95 // 在PanicRecovery(100)之后SecurityHeaders(85)之前执行
} }
// Handle 返回中间件处理函数 // Handle 返回中间件处理函数
@@ -39,22 +39,43 @@ func (m *CORSMiddleware) Handle() gin.HandlerFunc {
} }
} }
// 获取CORS配置
origins := m.getAllowedOrigins()
methods := m.getAllowedMethods()
headers := m.getAllowedHeaders()
config := cors.Config{ config := cors.Config{
AllowAllOrigins: false, AllowAllOrigins: false,
AllowOrigins: m.getAllowedOrigins(), AllowOrigins: origins,
AllowMethods: m.getAllowedMethods(), AllowMethods: methods,
AllowHeaders: m.getAllowedHeaders(), AllowHeaders: headers,
ExposeHeaders: []string{ ExposeHeaders: []string{
"Content-Length", "Content-Length",
"Content-Type", "Content-Type",
"X-Request-ID", "X-Request-ID",
"X-Response-Time", "X-Response-Time",
"Access-Control-Allow-Origin",
"Access-Control-Allow-Methods",
"Access-Control-Allow-Headers",
}, },
AllowCredentials: true, AllowCredentials: true,
MaxAge: 86400, // 24小时 MaxAge: 86400, // 24小时
// 增加Chrome兼容性
AllowWildcard: false,
AllowBrowserExtensions: false,
} }
return cors.New(config) // 创建CORS中间件
corsMiddleware := cors.New(config)
// 返回包装后的中间件
return func(c *gin.Context) {
// 调用实际的CORS中间件
corsMiddleware(c)
// 继续处理下一个中间件或处理器
c.Next()
}
} }
// IsGlobal 是否为全局中间件 // IsGlobal 是否为全局中间件