+ 跨域访问
This commit is contained in:
parent
2bcb92e221
commit
3cb780c631
1 changed files with 17 additions and 1 deletions
|
|
@ -13,7 +13,8 @@ import (
|
||||||
|
|
||||||
// Guards 服务中间件
|
// Guards 服务中间件
|
||||||
var Guards = map[string]gin.HandlerFunc{
|
var Guards = map[string]gin.HandlerFunc{
|
||||||
"bearer-jwt": bearerJWT, // JWT 鉴权
|
"bearer-jwt": bearerJWT, // JWT 鉴权
|
||||||
|
"cross-domain": crossDomain, // 跨域许可
|
||||||
}
|
}
|
||||||
|
|
||||||
// JWT 鉴权
|
// JWT 鉴权
|
||||||
|
|
@ -51,3 +52,18 @@ func bearerJWT(c *gin.Context) {
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
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()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue