Merge branch 'r/paopao-ce-plus' into r/paopao-ce-xtra

r/paopao-ce-xtra
Michael Li 1 year ago
commit a62b059aae
No known key found for this signature in database

@ -25,7 +25,6 @@ func JWT() gin.HandlerFunc {
token = s
} else {
token = c.GetHeader("Authorization")
// 验证前端传过来的token格式不为空开头为Bearer
if token == "" || !strings.HasPrefix(token, "Bearer ") {
response := app.NewResponse(c)
@ -33,47 +32,41 @@ func JWT() gin.HandlerFunc {
c.Abort()
return
}
// 验证通过提取有效部分除去Bearer)
token = token[7:]
}
if token == "" {
ecode = xerror.InvalidParams
if token != "" {
if claims, err := app.ParseToken(token); err == nil {
// 加载用户信息
if user, err := ums.GetUserByID(claims.UID); err == nil {
// 强制下线机制
if (conf.JWTSetting.Issuer + ":" + user.Salt) == claims.Issuer {
c.Set("USER", user)
c.Set("UID", claims.UID)
c.Set("USERNAME", claims.Username)
} else {
claims, err := app.ParseToken(token)
if err != nil {
switch err.(*jwt.ValidationError).Errors {
case jwt.ValidationErrorExpired:
ecode = xerror.UnauthorizedTokenTimeout
default:
ecode = xerror.UnauthorizedTokenError
}
} else {
c.Set("UID", claims.UID)
c.Set("USERNAME", claims.Username)
// 加载用户信息
user, err := ums.GetUserByID(claims.UID)
if err == nil {
c.Set("USER", user)
} else {
ecode = xerror.UnauthorizedAuthNotExist
}
// 强制下线机制
if (conf.JWTSetting.Issuer + ":" + user.Salt) != claims.Issuer {
} else {
switch err.(*jwt.ValidationError).Errors {
case jwt.ValidationErrorExpired:
ecode = xerror.UnauthorizedTokenTimeout
default:
ecode = xerror.UnauthorizedTokenError
}
}
} else {
ecode = xerror.InvalidParams
}
if ecode != xerror.Success {
response := app.NewResponse(c)
response.ToErrorResponse(ecode)
c.Abort()
return
}
c.Next()
}
}
@ -94,11 +87,11 @@ func JwtLoose() gin.HandlerFunc {
}
if len(token) > 0 {
if claims, err := app.ParseToken(token); err == nil {
c.Set("UID", claims.UID)
c.Set("USERNAME", claims.Username)
// 加载用户信息
user, err := ums.GetUserByID(claims.UID)
if err == nil && (conf.JWTSetting.Issuer+":"+user.Salt) == claims.Issuer {
c.Set("UID", claims.UID)
c.Set("USERNAME", claims.Username)
c.Set("USER", user)
}
}

Loading…
Cancel
Save