🎨 修改鉴权相关中间件

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

@@ -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)