From d4b62e5dcaf10b85600c13de232bb5173a11d4fc Mon Sep 17 00:00:00 2001 From: jiangshuai <291802688@qq.com> Date: 星期二, 06 二月 2024 10:14:46 +0800 Subject: [PATCH] 拦截器 --- middleware/reset_pwd.go | 25 +++++++++++++++++++++++++ request/jwt.go | 9 +++++---- pkg/ecode/code.go | 1 + router/router.go | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/middleware/reset_pwd.go b/middleware/reset_pwd.go new file mode 100644 index 0000000..f35ae56 --- /dev/null +++ b/middleware/reset_pwd.go @@ -0,0 +1,25 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "wms/pkg/contextx" + "wms/pkg/ecode" + "wms/request" +) + +func VerifyResetPwd() gin.HandlerFunc { + return func(c *gin.Context) { + ctx := new(contextx.Context).SetCtx(c) + params, ok := c.Get("claims") + if !ok { + c.Abort() + return + } + claims := params.(*request.CustomClaims) + if !claims.ModifiedPwd { + ctx.Fail(ecode.ResetPwd) + c.Abort() + return + } + } +} diff --git a/pkg/ecode/code.go b/pkg/ecode/code.go index aa74251..d7fd60b 100644 --- a/pkg/ecode/code.go +++ b/pkg/ecode/code.go @@ -18,4 +18,5 @@ JWTEmpty = 2013 // JWT涓虹┖ JWTExpire = 2014 // JWT杩囨湡 JWTParseErr = 2015 // JWT瑙f瀽澶辫触 + ResetPwd = 2036 //璐﹀彿璁剧疆瀵嗙爜 ) diff --git a/request/jwt.go b/request/jwt.go index dece0f1..da7fb70 100644 --- a/request/jwt.go +++ b/request/jwt.go @@ -13,8 +13,9 @@ } type BaseClaims struct { - UserId string - Username string - ParentId string - UserType constvar.UserType + UserId string + Username string + ParentId string + UserType constvar.UserType + ModifiedPwd bool } diff --git a/router/router.go b/router/router.go index e7565eb..d6a01d0 100644 --- a/router/router.go +++ b/router/router.go @@ -21,6 +21,7 @@ urlPrefix := "/api-wms/v1" r.Use(middleware.JWTAuth()) + r.Use(middleware.VerifyResetPwd()) // 缁勭粐绠$悊 departmentController := new(controllers.DepartmentController) -- Gitblit v1.8.0