🎨 修改鉴权相关中间件
This commit is contained in:
parent
44f1b5d4b5
commit
ed3c15fbb6
@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"git.echol.cn/loser/lckt/global"
|
||||
"git.echol.cn/loser/lckt/utils"
|
||||
"git.echol.cn/loser/lckt/utils/user_jwt"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -15,7 +16,7 @@ import (
|
||||
func UserJWTAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
|
||||
token := utils.GetToken(c)
|
||||
token := user_jwt.GetToken(c)
|
||||
if token == "" {
|
||||
response.NoAuth("未登录或非法访问", c)
|
||||
c.Abort()
|
||||
@ -28,12 +29,12 @@ func UserJWTAuth() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
if errors.Is(err, utils.TokenExpired) {
|
||||
response.NoAuth("授权已过期", c)
|
||||
utils.ClearToken(c)
|
||||
user_jwt.ClearToken(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
response.NoAuth(err.Error(), c)
|
||||
utils.ClearToken(c)
|
||||
user_jwt.ClearToken(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@ -46,7 +47,7 @@ func UserJWTAuth() gin.HandlerFunc {
|
||||
newClaims, _ := j.ParseToken(newToken)
|
||||
c.Header("new-token", newToken)
|
||||
c.Header("new-expires-at", strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10))
|
||||
utils.SetToken(c, newToken, int(dr.Seconds()))
|
||||
user_jwt.SetToken(c, newToken, int(dr.Seconds()))
|
||||
if global.GVA_CONFIG.System.UseMultipoint {
|
||||
// 记录新的活跃jwt
|
||||
_ = jwtService.SetRedisJWT(newToken, newClaims.Username)
|
||||
|
@ -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是否为规定结构")
|
||||
|
@ -12,41 +12,41 @@ import (
|
||||
)
|
||||
|
||||
func ClearToken(c *gin.Context) {
|
||||
// 增加cookie app-token 向来源的web添加
|
||||
// 增加cookie Authorization 向来源的web添加
|
||||
host, _, err := net.SplitHostPort(c.Request.Host)
|
||||
if err != nil {
|
||||
host = c.Request.Host
|
||||
}
|
||||
|
||||
if net.ParseIP(host) != nil {
|
||||
c.SetCookie("app-token", "", -1, "/", "", false, false)
|
||||
c.SetCookie("Authorization", "", -1, "/", "", false, false)
|
||||
} else {
|
||||
c.SetCookie("app-token", "", -1, "/", host, false, false)
|
||||
c.SetCookie("Authorization", "", -1, "/", host, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
func SetToken(c *gin.Context, token string, maxAge int) {
|
||||
// 增加cookie app-token 向来源的web添加
|
||||
// 增加cookie Authorization 向来源的web添加
|
||||
host, _, err := net.SplitHostPort(c.Request.Host)
|
||||
if err != nil {
|
||||
host = c.Request.Host
|
||||
}
|
||||
|
||||
if net.ParseIP(host) != nil {
|
||||
c.SetCookie("app-token", token, maxAge, "/", "", false, false)
|
||||
c.SetCookie("Authorization", token, maxAge, "/", "", false, false)
|
||||
} else {
|
||||
c.SetCookie("app-token", token, maxAge, "/", host, false, false)
|
||||
c.SetCookie("Authorization", token, maxAge, "/", host, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
func GetToken(c *gin.Context) string {
|
||||
token := c.Request.Header.Get("app-token")
|
||||
token := c.Request.Header.Get("Authorization")
|
||||
if token == "" {
|
||||
j := NewUserJWT()
|
||||
token, _ = c.Cookie("app-token")
|
||||
token, _ = c.Cookie("Authorization")
|
||||
claims, err := j.ParseToken(token)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在app-token且claims是否为规定结构")
|
||||
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在Authorization且claims是否为规定结构")
|
||||
return token
|
||||
}
|
||||
SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||
@ -59,7 +59,7 @@ func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) {
|
||||
j := NewUserJWT()
|
||||
claims, err := j.ParseToken(token)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析信息失败, 请检查请求头是否存在app-token且claims是否为规定结构")
|
||||
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析信息失败, 请检查请求头是否存在Authorization且claims是否为规定结构")
|
||||
}
|
||||
return claims, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user