package cors import ( "net/http" "strings" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) func Cors() gin.HandlerFunc { config := cors.DefaultConfig() config.AddAllowMethods(http.MethodDelete, http.MethodOptions, http.MethodPatch) // x-requested-with antd上传组件需要 config.AddAllowHeaders("Authorization", "X-Require-Cookie", "X-Device", "x-requested-with") config.AllowCredentials = true config.AllowOriginFunc = func(origin string) bool { if gin.Mode() == gin.DebugMode { return true } if strings.Contains(origin, "links123.com") { return true } return false } return cors.New(config) }