| | |
| | | |
| | | func JWTAuth2() gin.HandlerFunc { |
| | | return func(c *gin.Context) { |
| | | ctx := new(contextx.Context).SetCtx(c) |
| | | // 我们这里jwt鉴权取头部信息 Authorization 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录 |
| | | token := c.Request.Header.Get("Authorization") |
| | | if token == "" { |
| | | c.Next() |
| | | ctx.Fail(ecode.JWTEmpty) |
| | | c.Abort() |
| | | return |
| | | } |
| | | slices := strings.Split(token, " ") |
| | |
| | | c.Next() |
| | | return |
| | | } |
| | | |
| | | userInfo := service.GetUserBaseCache(claims.UserId) |
| | | if userInfo == nil { |
| | | SyncUserInfo() |
| | | userInfo = service.GetUserBaseCache(claims.UserId) |
| | | } |
| | | claims.CrmUserId = userInfo.UserId |
| | | claims.NickName = userInfo.NickName |
| | | c.Set("claims", claims) |
| | | c.Next() |
| | | if CheckAuth(c.Request.RequestURI, token) { |
| | | c.Next() |
| | | } else { |
| | | ctx.Fail(ecode.JWTDisabled) |
| | | c.Abort() |
| | | return |
| | | } |
| | | } |
| | | } |