From cc13b473ae85be4f9a1b3fe617083609612a4de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=90=89=E5=85=86?= <303176530@qq.com> Date: Mon, 25 Oct 2021 22:19:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=87=BAjwt=E7=9A=84issuer=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E5=88=B0=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0claims=E7=9A=84=E7=BB=9F=E4=B8=80=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/v1/system/sys_user.go | 15 +++------------ server/config/jwt.go | 1 + server/model/system/request/jwt.go | 8 ++++++-- server/utils/jwt.go | 14 ++++++++++++++ web/src/view/systemTools/system/system.vue | 3 +++ 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/server/api/v1/system/sys_user.go b/server/api/v1/system/sys_user.go index 8a59407a..c612ba66 100644 --- a/server/api/v1/system/sys_user.go +++ b/server/api/v1/system/sys_user.go @@ -1,9 +1,6 @@ package system import ( - "strconv" - "time" - "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" "github.com/flipped-aurora/gin-vue-admin/server/model/common/response" @@ -11,8 +8,8 @@ import ( systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response" "github.com/flipped-aurora/gin-vue-admin/server/utils" + "strconv" - "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "github.com/go-redis/redis/v8" "go.uber.org/zap" @@ -47,19 +44,13 @@ func (b *BaseApi) Login(c *gin.Context) { // 登录以后签发jwt func (b *BaseApi) tokenNext(c *gin.Context, user system.SysUser) { j := &utils.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名 - claims := systemReq.CustomClaims{ + claims := j.CreateClaims(systemReq.BaseClaims{ UUID: user.UUID, ID: user.ID, NickName: user.NickName, Username: user.Username, AuthorityId: user.AuthorityId, - BufferTime: global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失 - StandardClaims: jwt.StandardClaims{ - NotBefore: time.Now().Unix() - 1000, // 签名生效时间 - ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天 配置文件 - Issuer: "qmPlus", // 签名的发行者 - }, - } + }) token, err := j.CreateToken(claims) if err != nil { global.GVA_LOG.Error("获取token失败!", zap.Any("err", err)) diff --git a/server/config/jwt.go b/server/config/jwt.go index 0ac7168f..04de5db2 100644 --- a/server/config/jwt.go +++ b/server/config/jwt.go @@ -4,4 +4,5 @@ type JWT struct { SigningKey string `mapstructure:"signing-key" json:"signingKey" yaml:"signing-key"` // jwt签名 ExpiresTime int64 `mapstructure:"expires-time" json:"expiresTime" yaml:"expires-time"` // 过期时间 BufferTime int64 `mapstructure:"buffer-time" json:"bufferTime" yaml:"buffer-time"` // 缓冲时间 + Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` } diff --git a/server/model/system/request/jwt.go b/server/model/system/request/jwt.go index 7aa16429..bb673f66 100644 --- a/server/model/system/request/jwt.go +++ b/server/model/system/request/jwt.go @@ -7,11 +7,15 @@ import ( // Custom claims structure type CustomClaims struct { + BaseClaims + BufferTime int64 + jwt.StandardClaims +} + +type BaseClaims struct { UUID uuid.UUID ID uint Username string NickName string AuthorityId string - BufferTime int64 - jwt.StandardClaims } diff --git a/server/utils/jwt.go b/server/utils/jwt.go index 098d0f3c..196e44d4 100644 --- a/server/utils/jwt.go +++ b/server/utils/jwt.go @@ -5,6 +5,7 @@ import ( "github.com/dgrijalva/jwt-go" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" + "time" ) type JWT struct { @@ -24,6 +25,19 @@ func NewJWT() *JWT { } } +func (j *JWT) CreateClaims(baseClaims request.BaseClaims) request.CustomClaims { + claims := request.CustomClaims{ + BaseClaims: baseClaims, + BufferTime: global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失 + StandardClaims: jwt.StandardClaims{ + NotBefore: time.Now().Unix() - 1000, // 签名生效时间 + ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天 配置文件 + Issuer: global.GVA_CONFIG.JWT.Issuer, // 签名的发行者 + }, + } + return claims +} + // 创建一个token func (j *JWT) CreateToken(claims request.CustomClaims) (string, error) { token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) diff --git a/web/src/view/systemTools/system/system.vue b/web/src/view/systemTools/system/system.vue index 5d64848b..47df0959 100644 --- a/web/src/view/systemTools/system/system.vue +++ b/web/src/view/systemTools/system/system.vue @@ -43,6 +43,9 @@ + + +