From 018d8fadaf3062304e3f6d90b74f431e3ccb470d Mon Sep 17 00:00:00 2001 From: hreenshan112 Date: Thu, 13 Feb 2025 13:38:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0JWT=E9=89=B4=E6=9D=83?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/common/shiyan.go | 10 ++++- initialization/router.go | 7 +++ routers/appShiyan/routers.go | 1 + utils/jwt.go | 82 +++++++++++++++++++++++++++++++++++- 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/controller/common/shiyan.go b/controller/common/shiyan.go index 854c373..ada1cc8 100644 --- a/controller/common/shiyan.go +++ b/controller/common/shiyan.go @@ -41,11 +41,17 @@ func (a *ApiMethod) Index(c *gin.Context) { } func (a *ApiMethod) JieXiJwt(c *gin.Context) { - + strinf := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOiIzMDA0NTAiLCJVVUlEIjoiMzUwNTc2MDg1NzI2OTI4ODk2IiwiZXhwIjoxNzQwMDE1ODUyLCJpYXQiOjE3Mzk0MTEwNTIsImlzcyI6ImFkbWluIiwic3ViIjoiQXBwSnd0VG9rZW4ifQ.UZQSs6ScQ5lskaPgCs7eB1B9AW9G8O9iHmDTQYKssFY" + aa, b, d := utils.ParseToken(strinf) + sendMap := make(map[string]interface{}) + sendMap["token"] = aa + sendMap["cla"] = b + sendMap["err"] = d + formatoutput.Result(0, sendMap, c) } /* -* +*{"conversationId":"3d24a4a6-fa93-4770-8dd6-69fa3545afbd","source":"instruct"} @ 作者: 秦东 @ 时间: 2024-11-08 15:20:28 @ 功能: 实验图片改变格式 diff --git a/initialization/router.go b/initialization/router.go index a94c28e..a29e636 100644 --- a/initialization/router.go +++ b/initialization/router.go @@ -2,6 +2,7 @@ package initialization import ( "appNewPlatform/routers" + "appNewPlatform/utils" "io/ioutil" "github.com/gin-gonic/gin" @@ -41,6 +42,12 @@ func InitializeRouter() *gin.Engine { { wsApiRouter.RouterGroup(appLoadRouterGroup) } + } + //采用jwt鉴权 + jwtRouterGroup := router.Group("") + jwtRouterGroup.Use(utils.AuthMiddleware()) + { + } return router } diff --git a/routers/appShiyan/routers.go b/routers/appShiyan/routers.go index 4d2172d..6e836b0 100644 --- a/routers/appShiyan/routers.go +++ b/routers/appShiyan/routers.go @@ -15,5 +15,6 @@ func (a *ApiRouter) RouterGroup(router *gin.RouterGroup) { apiRouter.POST("", methodBinding.Index) //入口 apiRouter.GET("visitWebserviceInterface", methodBinding.VisitWebserviceInterface) //访问webservice接口 + apiRouter.POST("jieXiJwt", methodBinding.JieXiJwt) } } diff --git a/utils/jwt.go b/utils/jwt.go index aa59704..901903b 100644 --- a/utils/jwt.go +++ b/utils/jwt.go @@ -3,9 +3,12 @@ package utils import ( "appNewPlatform/appConstant" "appNewPlatform/generalmethod" + "appNewPlatform/utils/formatoutput" + "fmt" "strconv" "time" + "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" ) @@ -60,7 +63,9 @@ func ReleaseToken(userKey string) (token string, err error) { @ 返回值 - # + #jwt.Token + #jwt.Token + #error @ 方法原型 @@ -73,3 +78,78 @@ func ParseToken(tokenString string) (*jwt.Token, *Claims, error) { }) return token, claims, err } + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-13 10:44:02 +@ 功能: 验证jwt令牌 +@ 参数 + + #tokenString 令牌 + +@ 返回值 + + #Claims 解析后数据 + #error 信息 + +@ 方法原型 + + # +*/ +func ValidateToken(tokenString string) (*Claims, error) { + token, claims, err := ParseToken(tokenString) + if err != nil { + return nil, err + } + if !token.Valid { + return nil, fmt.Errorf("无效的令牌") + } + if time.Now().Unix() > claims.ExpiresAt { + return nil, fmt.Errorf("令牌已过期") + } + return claims, nil +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-13 11:36:59 +@ 功能: +@ 参数 + + # + +@ 返回值 + + # + +@ 方法原型 + + # +*/ +func AuthMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + tokenString := c.GetHeader("Authorization") + if tokenString == "" { + formatoutput.Result(300, tokenString, c, "未获得授权!") + c.Abort() + return + } + if len(tokenString) > 7 && tokenString[:7] == "Bearer " { + tokenString = tokenString[7:] + } else { + formatoutput.Result(300, tokenString, c, "授权标头无效!") + c.Abort() + return + } + _, claims, err := ParseToken(tokenString) + if err != nil { + formatoutput.Result(300, tokenString, c, "无效的令牌!") + c.Abort() + return + } + c.Set("username", claims.UserId) + c.Next() + } +}