🎨 修改鉴权相关中间件

This commit is contained in:
2025-05-09 11:16:18 +08:00
parent 44f1b5d4b5
commit ed3c15fbb6
3 changed files with 21 additions and 20 deletions

View File

@@ -19,9 +19,9 @@ func ClearToken(c *gin.Context) {
}
if net.ParseIP(host) != nil {
c.SetCookie("app-token", "", -1, "/", "", false, false)
c.SetCookie("x-token", "", -1, "/", "", false, false)
} else {
c.SetCookie("app-token", "", -1, "/", host, false, false)
c.SetCookie("x-token", "", -1, "/", host, false, false)
}
}
@@ -33,17 +33,17 @@ func SetToken(c *gin.Context, token string, maxAge int) {
}
if net.ParseIP(host) != nil {
c.SetCookie("app-token", token, maxAge, "/", "", false, false)
c.SetCookie("x-token", token, maxAge, "/", "", false, false)
} else {
c.SetCookie("app-token", token, maxAge, "/", host, false, false)
c.SetCookie("x-token", token, maxAge, "/", host, false, false)
}
}
func GetToken(c *gin.Context) string {
token := c.Request.Header.Get("app-token")
token := c.Request.Header.Get("x-token")
if token == "" {
j := NewJWT()
token, _ = c.Cookie("app-token")
token, _ = c.Cookie("x-token")
claims, err := j.ParseToken(token)
if err != nil {
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")