mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
22 lines
693 B
Go
22 lines
693 B
Go
|
|
package middleware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-contrib/cors"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CorsMiddleware 处理跨域请求
|
||
|
|
// 允许 Vue 等前端分离架构在开发和生产环境下访问后端 API
|
||
|
|
func CorsMiddleware() gin.HandlerFunc {
|
||
|
|
return cors.New(cors.Config{
|
||
|
|
AllowOriginFunc: func(origin string) bool { return true }, // 允许所有来源
|
||
|
|
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
|
||
|
|
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization", "X-CSRF-Token", "Accept"},
|
||
|
|
ExposeHeaders: []string{"Content-Length"},
|
||
|
|
AllowCredentials: true,
|
||
|
|
MaxAge: 12 * time.Hour,
|
||
|
|
})
|
||
|
|
}
|