+ 跨域访问

This commit is contained in:
Max 2021-12-30 15:15:13 +08:00
parent 2bcb92e221
commit 3cb780c631

View file

@ -13,7 +13,8 @@ import (
// Guards 服务中间件
var Guards = map[string]gin.HandlerFunc{
"bearer-jwt": bearerJWT, // JWT 鉴权
"bearer-jwt": bearerJWT, // JWT 鉴权
"cross-domain": crossDomain, // 跨域许可
}
// JWT 鉴权
@ -51,3 +52,18 @@ func bearerJWT(c *gin.Context) {
c.Abort()
return
}
// crossDomain 跨域访问
func crossDomain(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}