20 lines
941 B
Go
20 lines
941 B
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func Cors() gin.HandlerFunc {
|
|
return cors.New(cors.Config{
|
|
AllowOrigins: []string{"*"}, // 允许所有来源(生产环境不建议使用)
|
|
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, // 允许的请求方法
|
|
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"}, // 允许的请求头部
|
|
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Authorization"}, // 允许客户端获取的响应头部
|
|
AllowCredentials: true, // 允许携带 Cookie
|
|
MaxAge: 12 * time.Hour, // 预检请求的缓存时间
|
|
})
|
|
}
|