🔧Chore: 配置跨域

This commit is contained in:
2026-03-24 11:56:25 +08:00
parent 0200ca99b2
commit fe966d876f
2 changed files with 23 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
package middlewares
import (
"net/http"
"github.com/gin-gonic/gin"
)
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*") // 可替换为具体域名
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization")
c.Header("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}