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