Skip to content

Commit e197568

Browse files
author
pixel
committed
将JWT过期时间 刷新时间 放置进入config
1 parent f914f96 commit e197568

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

server/api/v1/sys_user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ func tokenNext(c *gin.Context, user model.SysUser) {
5050
NickName: user.NickName,
5151
Username: user.Username,
5252
AuthorityId: user.AuthorityId,
53-
BufferTime: 60 * 60 * 24, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
53+
BufferTime: global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
5454
StandardClaims: jwt.StandardClaims{
55-
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
56-
ExpiresAt: time.Now().Unix() + 60*60*24*7, // 过期时间 7天
57-
Issuer: "qmPlus", // 签名的发行者
55+
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
56+
ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天 配置文件
57+
Issuer: "qmPlus", // 签名的发行者
5858
},
5959
}
6060
token, err := j.CreateToken(claims)

server/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# jwt configuration
44
jwt:
55
signing-key: 'qmPlus'
6+
expires-time: 604800
7+
buffer-time: 86400
68

79
# zap logger configuration
810
zap:

server/config/jwt.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package config
22

33
type JWT struct {
4-
SigningKey string `mapstructure:"signing-key" json:"signingKey" yaml:"signing-key"`
4+
SigningKey string `mapstructure:"signing-key" json:"signingKey" yaml:"signing-key"`
5+
ExpiresTime int64 `mapstructure:"expires-time" json:"expiresTime" yaml:"expires-time"`
6+
BufferTime int64 `mapstructure:"buffer-time" json:"bufferTime" yaml:"buffer-time"`
57
}

server/middleware/jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func JWTAuth() gin.HandlerFunc {
4747
c.Abort()
4848
}
4949
if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime {
50-
claims.ExpiresAt = time.Now().Unix() + 60*60*24*7
50+
claims.ExpiresAt = time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime
5151
newToken, _ := j.CreateToken(*claims)
5252
newClaims, _ := j.ParseToken(newToken)
5353
c.Header("new-token", newToken)

server/service/jwt_black_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GetRedisJWT(userName string) (err error, redisJWT string) {
4949

5050
func SetRedisJWT(jwt string, userName string) (err error) {
5151
// 此处过期时间等于jwt过期时间
52-
timer := 60 * 60 * 24 * 7 * time.Second
52+
timer := time.Duration(global.GVA_CONFIG.JWT.ExpiresTime) * time.Second
5353
err = global.GVA_REDIS.Set(userName, jwt, timer).Err()
5454
return err
5555
}

0 commit comments

Comments
 (0)