Skip to content

Commit

Permalink
fix: Jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
lejianwen committed Jan 15, 2025
1 parent a33be66 commit a951b98
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion http/middleware/rustauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RustAuth() gin.HandlerFunc {
//验证token

//检查是否设置了jwt key
if global.Config.Jwt.Key != "" {
if len(global.Jwt.Key) > 0 {
uid, _ := service.AllService.UserService.VerifyJWT(token)
if uid == 0 {
c.JSON(401, gin.H{
Expand Down
6 changes: 5 additions & 1 deletion lib/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func NewJwt(key string, tokenExpireDuration time.Duration) *Jwt {
}

func (s *Jwt) GenerateToken(userId uint) string {
if len(s.Key) == 0 {
fmt.Println("jwt key is nil")
return ""
}
t := jwt.NewWithClaims(jwt.SigningMethodHS256,
UserClaims{
UserId: userId,
Expand All @@ -33,7 +37,7 @@ func (s *Jwt) GenerateToken(userId uint) string {
})
token, err := t.SignedString(s.Key)
if err != nil {
fmt.Println(err)
fmt.Printf("jwt token generate error: %v", err)
return ""
}
return token
Expand Down
2 changes: 1 addition & 1 deletion service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (us *UserService) InfoByAccessToken(token string) (*model.User, *model.User

// GenerateToken 生成token
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 utils.Md5(u.Username + time.Now().String())
Expand Down

0 comments on commit a951b98

Please sign in to comment.