Browse Source

提出jwt的issuer签名到配置文件 增加claims的统一创建方法

main
蒋吉兆 3 years ago
parent
commit
cc13b473ae
  1. 15
      server/api/v1/system/sys_user.go
  2. 1
      server/config/jwt.go
  3. 8
      server/model/system/request/jwt.go
  4. 14
      server/utils/jwt.go
  5. 3
      web/src/view/systemTools/system/system.vue

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

1
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"`
}

8
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
}

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

3
web/src/view/systemTools/system/system.vue

@ -43,6 +43,9 @@
<el-form-item label="缓冲期(秒)">
<el-input v-model="config.jwt.bufferTime" />
</el-form-item>
<el-form-item label="签发者">
<el-input v-model="config.jwt.issuer" />
</el-form-item>
</el-collapse-item>
<el-collapse-item title="Zap日志配置" name="3">
<el-form-item label="级别">

Loading…
Cancel
Save