package middleware import ( "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "time" ) func Cors() gin.HandlerFunc { return cors.New(cors.Config{ AllowOrigins: []string{"*"}, // 允许所有来源 AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Type", "Authorization"}, ExposeHeaders: []string{"Content-Length"}, AllowCredentials: true, MaxAge: 12 * time.Hour, }) }