JWT如何解析过期的token中的信息

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: JWT如何解析过期的token中的信息

 一、问题背景

  最近搭建springcloud的项目,项目采取了Jwt + spring security 来进行登录验证,Jwt token 锁定用户的失效时间,但是由于 jwt token特性导致token失效时间无法刷新,所以必须新创建一个token令牌,用来代替之前已失效token。

  (token失效时间无法刷新的原因是由于jwt创建token是根据jwt保存的相关信息来计算的,过期时间是其中的一个计算维度,所以一旦过期时间改了,那么生成的token值也就变了。)

  之后为了解决这个问题,结合了redis,将token值保存到redis中,用户操作后刷新redis的有效时间,这样如果jwt token失效了,再检查 redis 中保存token的key是否失效,如果没有失效,那么就重新创建jwt token ,失效了,就重新登录。

  我用一个过期token去调用接口,返回请求未授权。我查看token拦截的方法,发现是token解析异常,返回null导致接口返回未授权。token解析的最终处理都是parseJWT(jsonWebToken)这个方法,代码如下:

public static Claims parseJWT(String jsonWebToken) {
        try {
            return Jwts.parser().setSigningKey(Base64.getDecoder().decode(getBase64Security())).parseClaimsJws(jsonWebToken).getBody();
        } catch (Exception var2) {
            return null;
        }
    }

image.gif

二、问题原因

根据报错堆栈信息找到了DefaultJwtParser类中,找到了问题的原因。

传入过期token,debug调试发现是在DefaultJwtParser的parse(String jwt)方法中返回了ExpiredJwtException 过期异常。部分代码如下

boolean allowSkew = this.allowedClockSkewMillis > 0L;
                if (claims != null) {
                    Date now = this.clock.now();
                    long nowTime = now.getTime();
                    Date exp = claims.getExpiration();
                    String nbfVal;
                    SimpleDateFormat sdf;
                    if (exp != null) {
                        long maxTime = nowTime - this.allowedClockSkewMillis;
                        Date max = allowSkew ? new Date(maxTime) : now;
                        if (max.after(exp)) {
                            sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                            String expVal = sdf.format(exp);
                            nbfVal = sdf.format(now);
                            long differenceMillis = maxTime - exp.getTime();
                            String msg = "JWT expired at " + expVal + ". Current time: " + nbfVal + ", a difference of " + differenceMillis + " milliseconds.  Allowed clock skew: " + this.allowedClockSkewMillis + " milliseconds.";
                            throw new ExpiredJwtException((Header)header, claims, msg);
                        }
                    }
                    Date nbf = claims.getNotBefore();
                    if (nbf != null) {
                        long minTime = nowTime + this.allowedClockSkewMillis;
                        Date min = allowSkew ? new Date(minTime) : now;
                        if (min.before(nbf)) {
                            sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                            nbfVal = sdf.format(nbf);
                            String nowVal = sdf.format(now);
                            long differenceMillis = nbf.getTime() - minTime;
                            String msg = "JWT must not be accepted before " + nbfVal + ". Current time: " + nowVal + ", a difference of " + differenceMillis + " milliseconds.  Allowed clock skew: " + this.allowedClockSkewMillis + " milliseconds.";
                            throw new PrematureJwtException((Header)header, claims, msg);
                        }
                    }
                    this.validateExpectedClaims((Header)header, claims);
                }

image.gif

看到结尾的throw new ExpiredJwtException,我相信就找到了问题的关键,原来在在解析token并发现这个token已经过期了,它作出的反应是直接抛异常。

image.gif编辑

异常定义的构造方法中除了msg信息,还有claims和header信息。

检查claims发现,在异常之前token其实已经解析完毕。

这样也就代表着,抛出的这个异常 ExpiredJwtException 中有一个参数 claims 就是解析后的token,那么本次这个问题也就解决了。

  catch ExpiredJwtException 异常后,直接从异常中获取解析的数据即可。

三、方法处理

修改原来的parseJWT(jsonWebToken),代码如下:

public static Claims parseJWT(String jsonWebToken) {
        try {
            return Jwts.parser().setSigningKey(Base64.getDecoder().decode(getBase64Security())).parseClaimsJws(jsonWebToken).getBody();
        } catch (ExpiredJwtException var) {
            return  var.getClaims();
        } catch (PrematureJwtException var1) {
            return var1.getClaims();
        } catch (Exception var2) {
            return null;
        }
    }

image.gif

ExpiredJwtException 异常是超过token过期时间异常

PrematureJwtException 异常是早于token生效时间异议

附带判断token是否过期的方法代码:

public Boolean isTokenExpired(String token) {
        //不管是否过期,都返回claims对象
        Claims claims = parseJWT(token);
        Date expiration = claims.getExpiration();
        //和当前时间进行对比来判断是否过期
        return new Date(System.currentTimeMillis()).after(expiration);
    }

image.gif

最终测试,可以从过期的token中获取用户信息了,接口也不拦截过期token了(因为之间的拦截器代码是根据token能不能解析用户信息去拦截的)。


相关文章
|
2天前
|
JSON JavaScript 前端开发
❤Nodejs 第九章(token身份认证和express-jwt的安装认识)
【4月更文挑战第9天】Node.js第九章介绍了Token身份认证,特别是JWT(JSON Web Token)作为跨域认证的解决方案。JWT由Header、Payload和Signature三部分组成,用于在客户端和服务器间安全传输用户信息。前端收到JWT后存储在localStorage或sessionStorage中,并在请求头中发送。Express-JWT是一个中间件,用于解析JWT。基本用法包括设置secret和algorithms。注意安全问题,避免混合使用不同算法以防止降级攻击。
45 0
|
2天前
|
Python
查看DataFrame信息案例解析
该文介绍了如何使用pandas库查看DataFrame信息。首先,导入pandas并创建一个字典,将字典转换为DataFrame,展示了一组包含“姓名”、“年龄”和“城市”列的数据。之后,通过调用DataFrame的info()方法,显示了数据框的详细信息,包括行数、列数及每列的数据类型,如:3行3列,数据类型为1个int64和2个object。
14 0
|
2天前
|
Python
选择和过滤数据DataFrame信息案例解析
该文介绍了如何使用pandas处理DataFrame数据。首先,通过创建字典并调用pd.DataFrame()函数转换为DataFrame。接着展示了使用loc[]选择年龄大于30的记录,iloc[]选取特定位置行,以及query()根据字符串表达式筛选(年龄大于30且城市为北京)数据的方法。
67 1
|
2天前
|
Python
修改DataFrame信息案例解析
【4月更文挑战第9天】该文介绍了如何修改DataFrame信息,首先通过`pd.DataFrame()`将字典转换为DataFrame,然后利用`.loc[]`、`.iloc[]`和`.query()`方法修改特定条件的数据。示例中,更改了年龄大于30的值为31,更新了第1行和第3行数据,以及使用查询语句修改年龄大于30且城市为北京的记录。
18 0
|
2天前
|
Serverless Python
分组和聚合DataFrame信息案例解析
该文介绍了如何使用pandas对DataFrame进行分组和聚合操作。首先,通过创建字典并转换为DataFrame,展示了基础数据结构。接着,利用`groupby()`方法按城市字段进行数据分组,然后应用`mean()`函数计算各城市平均年龄,显示了聚合功能。此外,文中指出还可使用`sum()`、`count()`等其他聚合函数处理分组数据。
18 0
|
2天前
|
算法 Linux C++
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
35 0
|
2天前
|
存储 缓存 NoSQL
【Redis】Redis魔法:揭秘Key的自动消失术——过期删除机制解析
【Redis】Redis魔法:揭秘Key的自动消失术——过期删除机制解析
148 0
|
2天前
|
传感器 存储 监控
【物联网】液滴即信息:雨滴探测传感器实验解析降雨的密码
【物联网】液滴即信息:雨滴探测传感器实验解析降雨的密码
58 0
|
2天前
|
存储 NoSQL 前端开发
jwt与redis,把生成的token放入redis中进行临时存储
jwt与redis,把生成的token放入redis中进行临时存储
99 0
|
2天前
|
缓存 前端开发 Java
【二十八】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能
【二十八】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能
44 1

推荐镜像

更多