@@ -9,24 +9,22 @@ import (
99 "gin-vue-admin/service"
1010 "github.com/dgrijalva/jwt-go"
1111 "github.com/gin-gonic/gin"
12+ "strconv"
1213 "time"
1314)
1415
1516func JWTAuth () gin.HandlerFunc {
1617 return func (c * gin.Context ) {
1718 // 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localSstorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
1819 token := c .Request .Header .Get ("x-token" )
19- modelToken := model.JwtBlacklist {
20- Jwt : token ,
21- }
2220 if token == "" {
2321 response .Result (response .ERROR , gin.H {
2422 "reload" : true ,
2523 }, "未登录或非法访问" , c )
2624 c .Abort ()
2725 return
2826 }
29- if service .IsBlacklist (token , modelToken ) {
27+ if service .IsBlacklist (token ) {
3028 response .Result (response .ERROR , gin.H {
3129 "reload" : true ,
3230 }, "您的帐户异地登陆或令牌失效" , c )
@@ -50,6 +48,24 @@ func JWTAuth() gin.HandlerFunc {
5048 c .Abort ()
5149 return
5250 }
51+ if claims .ExpiresAt - time .Now ().Unix ()< claims .BufferTime {
52+ claims .ExpiresAt = time .Now ().Unix () + 60 * 60 * 24 * 7
53+ newToken ,_ := j .CreateToken (* claims )
54+ newClaims ,_ := j .ParseToken (newToken )
55+ c .Header ("new-token" ,newToken )
56+ c .Header ("new-expires-at" ,strconv .FormatInt (newClaims .ExpiresAt ,10 ))
57+ if global .GVA_CONFIG .System .UseMultipoint {
58+ err ,RedisJwtToken := service .GetRedisJWT (newClaims .Username )
59+ if err != nil {
60+ global .GVA_LOG .Error (err )
61+ }else {
62+ service .JsonInBlacklist (model.JwtBlacklist {Jwt : RedisJwtToken })
63+ //当之前的取成功时才进行拉黑操作
64+ }
65+ // 无论如何都要记录当前的活跃状态
66+ _ = service .SetRedisJWT (newToken ,newClaims .Username )
67+ }
68+ }
5369 c .Set ("claims" , claims )
5470 c .Next ()
5571 }
@@ -111,20 +127,20 @@ func (j *JWT) ParseToken(tokenString string) (*request.CustomClaims, error) {
111127}
112128
113129// 更新token
114- func (j * JWT ) RefreshToken (tokenString string ) (string , error ) {
115- jwt .TimeFunc = func () time.Time {
116- return time .Unix (0 , 0 )
117- }
118- token , err := jwt .ParseWithClaims (tokenString , & request.CustomClaims {}, func (token * jwt.Token ) (interface {}, error ) {
119- return j .SigningKey , nil
120- })
121- if err != nil {
122- return "" , err
123- }
124- if claims , ok := token .Claims .(* request.CustomClaims ); ok && token .Valid {
125- jwt .TimeFunc = time .Now
126- claims .StandardClaims .ExpiresAt = time .Now ().Add ( 1 * time . Hour ). Unix ()
127- return j .CreateToken (* claims )
128- }
129- return "" , TokenInvalid
130- }
130+ // func (j *JWT) RefreshToken(tokenString string) (string, error) {
131+ // jwt.TimeFunc = func() time.Time {
132+ // return time.Unix(0, 0)
133+ // }
134+ // token, err := jwt.ParseWithClaims(tokenString, &request.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
135+ // return j.SigningKey, nil
136+ // })
137+ // if err != nil {
138+ // return "", err
139+ // }
140+ // if claims, ok := token.Claims.(*request.CustomClaims); ok && token.Valid {
141+ // jwt.TimeFunc = time.Now
142+ // claims.StandardClaims.ExpiresAt = time.Now().Unix() + 60*60*24*7
143+ // return j.CreateToken(*claims)
144+ // }
145+ // return "", TokenInvalid
146+ // }
0 commit comments