mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-07 16:16:37 +08:00
[优化] 更新 CORS 配置以支持动态源和凭证
This commit is contained in:
@@ -1,12 +1,26 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"openflare/common"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CORS() gin.HandlerFunc {
|
func CORS() gin.HandlerFunc {
|
||||||
config := cors.DefaultConfig()
|
config := cors.DefaultConfig()
|
||||||
config.AllowOrigins = []string{"https://openflare.vercel.app", "http://localhost:3000"}
|
config.AllowCredentials = true
|
||||||
|
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization", "X-Agent-Token", "Accept"}
|
||||||
|
config.AllowOriginFunc = func(origin string) bool {
|
||||||
|
serverAddr := strings.TrimRight(common.ServerAddress, "/")
|
||||||
|
if serverAddr == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if origin == serverAddr {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
return cors.New(config)
|
return cors.New(config)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user