This commit is contained in:
lejianwen
2025-01-15 20:26:26 +08:00
parent cca191aad3
commit a4433e6113
3 changed files with 7 additions and 3 deletions

View File

@@ -32,7 +32,7 @@ func RustAuth() gin.HandlerFunc {
//验证token //验证token
//检查是否设置了jwt key //检查是否设置了jwt key
if global.Config.Jwt.Key != "" { if len(global.Jwt.Key) > 0 {
uid, _ := service.AllService.UserService.VerifyJWT(token) uid, _ := service.AllService.UserService.VerifyJWT(token)
if uid == 0 { if uid == 0 {
c.JSON(401, gin.H{ c.JSON(401, gin.H{

View File

@@ -24,6 +24,10 @@ func NewJwt(key string, tokenExpireDuration time.Duration) *Jwt {
} }
func (s *Jwt) GenerateToken(userId uint) string { func (s *Jwt) GenerateToken(userId uint) string {
if len(s.Key) == 0 {
fmt.Println("jwt key is nil")
return ""
}
t := jwt.NewWithClaims(jwt.SigningMethodHS256, t := jwt.NewWithClaims(jwt.SigningMethodHS256,
UserClaims{ UserClaims{
UserId: userId, UserId: userId,
@@ -33,7 +37,7 @@ func (s *Jwt) GenerateToken(userId uint) string {
}) })
token, err := t.SignedString(s.Key) token, err := t.SignedString(s.Key)
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("jwt token generate error: %v", err)
return "" return ""
} }
return token return token

View File

@@ -68,7 +68,7 @@ func (us *UserService) InfoByAccessToken(token string) (*model.User, *model.User
// GenerateToken 生成token // GenerateToken 生成token
func (us *UserService) GenerateToken(u *model.User) string { func (us *UserService) GenerateToken(u *model.User) string {
if global.Config.Jwt.Key != "" { if len(global.Jwt.Key) > 0 {
return global.Jwt.GenerateToken(u.Id) return global.Jwt.GenerateToken(u.Id)
} }
return utils.Md5(u.Username + time.Now().String()) return utils.Md5(u.Username + time.Now().String())