package middleware import ( "net/http" "github.com/gin-gonic/gin" ) // CorsMiddleware 跨域中间件 func CorsMiddleware() gin.HandlerFunc { return func(c *gin.Context) { method := c.Request.Method origin := c.Request.Header.Get("Origin") if origin != "" { // 允许所有来源,生产环境请修改为特定域名 c.Header("Access-Control-Allow-Origin", origin) c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-App-Sign") c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type") c.Header("Access-Control-Allow-Credentials", "true") } // 放行 OPTIONS 请求 if method == "OPTIONS" { c.AbortWithStatus(http.StatusNoContent) } c.Next() } }