From 02a2176f7c5733a4e4c4429c2028bbb86a967ce7 Mon Sep 17 00:00:00 2001 From: jiangshuai <291802688@qq.com> Date: 星期二, 06 二月 2024 10:13:38 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/aps/SRM --- middleware/jwt.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 39 insertions(+), 0 deletions(-) diff --git a/middleware/jwt.go b/middleware/jwt.go new file mode 100644 index 0000000..42ce6e0 --- /dev/null +++ b/middleware/jwt.go @@ -0,0 +1,39 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "srm/pkg/contextx" + "srm/pkg/ecode" + "strings" +) + +func JWTAuth() gin.HandlerFunc { + return func(c *gin.Context) { + ctx := new(contextx.Context).SetCtx(c) + // 鎴戜滑杩欓噷jwt閴存潈鍙栧ご閮ㄤ俊鎭� Authorization 鐧诲綍鏃跺洖杩斿洖token淇℃伅 杩欓噷鍓嶇闇�瑕佹妸token瀛樺偍鍒癱ookie鎴栬�呮湰鍦發ocalStorage涓� 涓嶈繃闇�瑕佽窡鍚庣鍗忓晢杩囨湡鏃堕棿 鍙互绾﹀畾鍒锋柊浠ょ墝鎴栬�呴噸鏂扮櫥褰� + token := c.Request.Header.Get("Authorization") + if token == "" { + ctx.Fail(ecode.JWTEmpty) + c.Abort() + return + } + slices := strings.Split(token, " ") + if len(slices) == 2 { + token = slices[1] + } + j := NewJWT() + // parseToken 瑙f瀽token鍖呭惈鐨勪俊鎭� + claims, err := j.ParseToken(token) + if err != nil { + if err == TokenExpired { + c.Next() + return + } + c.Next() + return + } + + c.Set("claims", claims) + c.Next() + } +} -- Gitblit v1.8.0