From 3aa2f63216fdc79e37bb4b1698f9fac9943e0e85 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 18 May 2025 14:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9jwt=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 2902f29..d60e74a 100644 --- a/main.go +++ b/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()}) } // 将用户信息添加到上下文中