From 3e1d3d63d0ae457e90cf1c3c788090dbb138eeb8 Mon Sep 17 00:00:00 2001 From: coward Date: Fri, 12 Jul 2024 17:18:04 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/jwt.go | 5 ++++- global/constant/redis_key.go | 4 ++-- http/middleware/authorization.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/component/jwt.go b/component/jwt.go index 5629e1e..e28cdac 100644 --- a/component/jwt.go +++ b/component/jwt.go @@ -68,7 +68,10 @@ func (JwtComponent) GenerateToken(userId, secret string, times ...time.Time) (to return "", nil, errors.New("生成token失败") } - client.Redis.Set(context.Background(), fmt.Sprintf("%s:%s", constant.UserToken, userId), token, 7*time.Hour) + client.Redis.Set(context.Background(), + fmt.Sprintf("%s:%s", constant.UserToken, userId), + token, + time.Duration(expireTime.Sub(time.Now()).Abs().Seconds())*time.Second) return } diff --git a/global/constant/redis_key.go b/global/constant/redis_key.go index a96af7f..5f64905 100644 --- a/global/constant/redis_key.go +++ b/global/constant/redis_key.go @@ -1,6 +1,6 @@ package constant const ( - Captcha = "captcha:" - UserToken = "token:" + Captcha = "captcha" + UserToken = "token" ) diff --git a/http/middleware/authorization.go b/http/middleware/authorization.go index a42bdb7..346a28f 100644 --- a/http/middleware/authorization.go +++ b/http/middleware/authorization.go @@ -65,7 +65,7 @@ func Authorization() gin.HandlerFunc { // 生成一个新token secret := component.JWT().GenerateSecret(user.Password, uuid.NewString(), time.Now().Local().String()) - tokenStr, _, err := component.JWT().GenerateToken(user.Id, secret, userClaims.ExpiresAt.Time, userClaims.IssuedAt.Time) + tokenStr, _, err := component.JWT().GenerateToken(user.Id, secret, userClaims.ExpiresAt.Time, time.Now().Local()) if err != nil { response.R(c).AuthorizationFailed("校验失败") c.Abort()