Browse Source

将JWT过期时间 刷新时间 放置进入config

main
pixel 4 years ago
committed by QM303176530
parent
commit
8c6937d813
  1. 8
      server/api/v1/sys_user.go
  2. 2
      server/config.yaml
  3. 4
      server/config/jwt.go
  4. 2
      server/middleware/jwt.go
  5. 2
      server/service/jwt_black_list.go

8
server/api/v1/sys_user.go

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

2
server/config.yaml

@ -3,6 +3,8 @@
# jwt configuration # jwt configuration
jwt: jwt:
signing-key: 'qmPlus' signing-key: 'qmPlus'
expires-time: 604800
buffer-time: 86400
# zap logger configuration # zap logger configuration
zap: zap:

4
server/config/jwt.go

@ -1,5 +1,7 @@
package config package config
type JWT struct { type JWT struct {
SigningKey string `mapstructure:"signing-key" json:"signingKey" yaml:"signing-key"`
SigningKey string `mapstructure:"signing-key" json:"signingKey" yaml:"signing-key"`
ExpiresTime int64 `mapstructure:"expires-time" json:"expiresTime" yaml:"expires-time"`
BufferTime int64 `mapstructure:"buffer-time" json:"bufferTime" yaml:"buffer-time"`
} }

2
server/middleware/jwt.go

@ -47,7 +47,7 @@ func JWTAuth() gin.HandlerFunc {
c.Abort() c.Abort()
} }
if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime { if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime {
claims.ExpiresAt = time.Now().Unix() + 60*60*24*7
claims.ExpiresAt = time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime
newToken, _ := j.CreateToken(*claims) newToken, _ := j.CreateToken(*claims)
newClaims, _ := j.ParseToken(newToken) newClaims, _ := j.ParseToken(newToken)
c.Header("new-token", newToken) c.Header("new-token", newToken)

2
server/service/jwt_black_list.go

@ -49,7 +49,7 @@ func GetRedisJWT(userName string) (err error, redisJWT string) {
func SetRedisJWT(jwt string, userName string) (err error) { func SetRedisJWT(jwt string, userName string) (err error) {
// 此处过期时间等于jwt过期时间 // 此处过期时间等于jwt过期时间
timer := 60 * 60 * 24 * 7 * time.Second
timer := time.Duration(global.GVA_CONFIG.JWT.ExpiresTime) * time.Second
err = global.GVA_REDIS.Set(userName, jwt, timer).Err() err = global.GVA_REDIS.Set(userName, jwt, timer).Err()
return err return err
} }
Loading…
Cancel
Save