修改jwt拦截处理,添加错误问题
This commit is contained in:
parent
858ffdc757
commit
3aa2f63216
16
main.go
16
main.go
|
|
@ -177,20 +177,8 @@ func JWTAuthMiddleware() gin.HandlerFunc {
|
|||
return proto.SigningKey, nil
|
||||
})
|
||||
// 错误处理
|
||||
if err != nil {
|
||||
if ve, ok := err.(*jwt.ValidationError); ok {
|
||||
switch {
|
||||
case ve.Errors&jwt.ValidationErrorMalformed != 0:
|
||||
c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Malformed token", "code": proto.TokenInvalid})
|
||||
case ve.Errors&jwt.ValidationErrorExpired != 0:
|
||||
c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Token expired", "code": proto.TokenExpired})
|
||||
case ve.Errors&jwt.ValidationErrorNotValidYet != 0:
|
||||
c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Token not active yet", "code": proto.TokenInvalid})
|
||||
default:
|
||||
c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Invalid token", "code": proto.TokenInvalid})
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || token.Valid == false {
|
||||
c.AbortWithStatusJSON(http.StatusOK, gin.H{"code": proto.TokenInvalid, "message": "unauthorized", "error": "token is invalid:" + err.Error()})
|
||||
}
|
||||
|
||||
// 将用户信息添加到上下文中
|
||||
|
|
|
|||
Loading…
Reference in New Issue