修改token过期时间
This commit is contained in:
parent
c44a1e565b
commit
bc02dc0285
16
utils/jwt.go
16
utils/jwt.go
@ -1,6 +1,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"time"
|
"time"
|
||||||
"vote-api/config"
|
"vote-api/config"
|
||||||
@ -13,11 +14,18 @@ type Token struct {
|
|||||||
|
|
||||||
// NewJWT GenerateJWT 生成JWT
|
// NewJWT GenerateJWT 生成JWT
|
||||||
func (t Token) NewJWT() (string, error) {
|
func (t Token) NewJWT() (string, error) {
|
||||||
ttl := time.Duration(config.C.Jwt.Ttl)
|
now := time.Now()
|
||||||
|
year, month, day := now.Date()
|
||||||
|
location := now.Location()
|
||||||
|
validTime := time.Date(year, month, day, 23, 59, 59, 0, location)
|
||||||
|
duration := validTime.Sub(now)
|
||||||
|
if duration < 0 {
|
||||||
|
return "", errors.New("登录失败")
|
||||||
|
}
|
||||||
|
|
||||||
t.RegisteredClaims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(ttl * time.Hour)) // 过期时间24小时
|
t.RegisteredClaims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(duration)) // 过期时间24小时
|
||||||
t.RegisteredClaims.IssuedAt = jwt.NewNumericDate(time.Now()) // 签发时间
|
t.RegisteredClaims.IssuedAt = jwt.NewNumericDate(time.Now()) // 签发时间
|
||||||
t.RegisteredClaims.NotBefore = jwt.NewNumericDate(time.Now()) // 生效时间
|
t.RegisteredClaims.NotBefore = jwt.NewNumericDate(time.Now()) // 生效时间
|
||||||
|
|
||||||
// 使用HS256签名算法
|
// 使用HS256签名算法
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, t)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, t)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user