| | |
| | | package middleware |
| | | |
| | | import ( |
| | | "aps_crm/conf" |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/contextx" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/logx" |
| | | "aps_crm/service" |
| | | "aps_crm/utils" |
| | | "github.com/gin-gonic/gin" |
| | | "strconv" |
| | | "strings" |
| | | "time" |
| | | ) |
| | | |
| | | var jwtService = service.ServiceGroup.JwtService |
| | |
| | | // response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c) |
| | | // c.Abort() |
| | | //} |
| | | //if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime { |
| | | // dr, _ := utils.ParseDuration(conf.Conf.JWT.ExpiresTime) |
| | | // claims.ExpiresAt = time.Now().Add(dr).Unix() |
| | | // newToken, _ := j.CreateTokenByOldToken(token, *claims) |
| | | // newClaims, _ := j.ParseToken(newToken) |
| | | // c.Header("new-token", newToken) |
| | | // c.Header("new-expires-at", strconv.FormatInt(newClaims.ExpiresAt, 10)) |
| | | // if conf.Conf.System.UseMultipoint { |
| | | // RedisJwtToken, err := jwtService.GetRedisJWT(newClaims.Username) |
| | | // if err != nil { |
| | | // logx.Errorf("get redis jwt failed err:%v", err) |
| | | // } else { // 当之前的取成功时才进行拉黑操作 |
| | | // _ = jwtService.JsonInBlacklist(model.JwtBlacklist{Jwt: RedisJwtToken}) |
| | | // } |
| | | // // 无论如何都要记录当前的活跃状态 |
| | | // _ = jwtService.SetRedisJWT(newToken, newClaims.Username) |
| | | // } |
| | | //} |
| | | if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime { |
| | | dr, _ := utils.ParseDuration(conf.Conf.JWT.ExpiresTime) |
| | | claims.ExpiresAt = time.Now().Add(dr).Unix() |
| | | newToken, _ := j.CreateTokenByOldToken(token, *claims) |
| | | newClaims, _ := j.ParseToken(newToken) |
| | | c.Header("new-token", newToken) |
| | | c.Header("new-expires-at", strconv.FormatInt(newClaims.ExpiresAt, 10)) |
| | | if conf.Conf.System.UseMultipoint { |
| | | RedisJwtToken, err := jwtService.GetRedisJWT(newClaims.Username) |
| | | if err != nil { |
| | | logx.Errorf("get redis jwt failed err:%v", err) |
| | | } else { // 当之前的取成功时才进行拉黑操作 |
| | | _ = jwtService.JsonInBlacklist(model.JwtBlacklist{Jwt: RedisJwtToken}) |
| | | } |
| | | // 无论如何都要记录当前的活跃状态 |
| | | _ = jwtService.SetRedisJWT(newToken, newClaims.Username) |
| | | } |
| | | } |
| | | c.Set("claims", claims) |
| | | c.Next() |
| | | } |
| | | } |
| | | |
| | | 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 == "" { |
| | | ctx.Fail(ecode.JWTEmpty) |
| | | c.Abort() |
| | | return |
| | | } |
| | | slices := strings.Split(token, " ") |
| | | if len(slices) == 2 { |
| | | token = slices[1] |
| | | } |
| | | j := utils.NewJWT() |
| | | // parseToken 解析token包含的信息 |
| | | claims, err := j.ParseToken(token) |
| | | if err != nil { |
| | | ctx.Fail(ecode.JWTDisabled) |
| | | c.Abort() |
| | | return |
| | | } |
| | | userInfo := service.GetUserBaseCache(claims.UserId) |
| | | if userInfo == nil { |
| | | SyncUserInfo([]string{claims.UserId}) |
| | | userInfo = service.GetUserBaseCache(claims.UserId) |
| | | } |
| | | if userInfo == nil { |
| | | ctx.Fail(ecode.JWTDisabled) |
| | | c.Abort() |
| | | return |
| | | } |
| | | |
| | | SetActiveTime(claims.UserId) |
| | | |
| | | claims.CrmUserId = userInfo.UserId |
| | | claims.NickName = userInfo.NickName |
| | | claims.SubUserIds = userInfo.SubUserIds |
| | | c.Set("claims", claims) |
| | | if CheckAuth(c.Request.URL.Path, token) { |
| | | c.Next() |
| | | } else { |
| | | ctx.Fail(ecode.JWTDisabled) |
| | | c.Abort() |
| | | return |
| | | } |
| | | } |
| | | } |