JWT工具类

工具开源地址
    欢迎大家搜索“小猴子的技术笔记”关注我的公众号,有问题可以及时和我交流。

    之前我们已经了解到了什么是JWT以及JWT的优点,那么怎么在项目中使用到JWT呢?首先我们需要在maven的项目中引入JWT的依赖:


<dependency>
  <groupId>com.auth0</groupId>
  <artifactId>java-jwt</artifactId>
  <version>3.12.0</version>
</dependency>

    成功引入jar包之后就可以进行token的生成了。之后需要指定一个加密的算法,也就是需要你自己提供一个秘钥串来进行加密。你可以把它理解为之前做MD5加密的时候加上的盐值。

Algorithm algorithm = Algorithm.HMAC256("this is your secret")

    我们有十种算法可以选择:
在这里插入图片描述
    然后我们就可以利用jar包中的方法,创建一个token并且附带上签名,之后就能够得到一个完整的token令牌。


public static void main(String[] args) {
    Algorithm algorithm = Algorithm.HMAC256("this is your secret");
    String token = JWT.create().sign(algorithm);
    System.out.println(token);
}
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.e30.svmNHFYzrAj6USTjLekz3CTFyEdmpRkE8A8x3CbPe1Q

    这仅仅是一个简单的token的生成,有的时候我们还想在token中传递我们的信息,就可以使用下面这个方法:

public static void main(String[] args) {
    Algorithm algorithm = Algorithm.HMAC256("this is your secret");
    String token = JWT.create().withClaim("name", "小猴子").sign(algorithm);
    System.out.println(token);
}

    如果你有很多条件需要传递就可以“withClaim()”多个值。需要注意的是:这里请不要传递敏感的信息以免token被破解,信息泄露。

    我们在开发中一个token肯定不可能一直使用,一定有个过期时间。那么在JWT中怎么定义token的过期时间呢?JWT内置了为我们设置token的过期时间策略,官网给提供了一个“withExpireAt()”的方法。需要传递一个日期参数来指定过期时间。
在这里插入图片描述

public static void main(String[] args) {
    Algorithm algorithm = Algorithm.HMAC256("this is your secret");
    JWTCreator.Builder builder = JWT.create().withClaim("name", "小猴子");
    String token = builder.withExpiresAt(new Date(System.currentTimeMillis() + 2000)).sign(algorithm);
    System.out.println(token);
}

    生成token:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi5bCP54y05a2QIiwiZXhwIjoxNjEwODgzOTEzfQ.sAqi1xhajwKCxuv5sx8EwlcpbflsJs-k-67Nqr8PSYg

    上面我们自定义了过期的时间,是当前时间+2000毫秒之后过期,也就是2秒之后过期。那么怎么来查看token是否已经过期呢?我们需要用到token的校验:

public static void main(String[] args) {
    Algorithm algorithm = Algorithm.HMAC256("this is your secret");
    JWTVerifier verifier = JWT.require(algorithm).build();
    DecodedJWT decoded = verifier.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi5bCP54y05a2QIiwiZXhwIjoxNjEwODgzOTEzfQ.sAqi1xhajwKCxuv5sx8EwlcpbflsJs-k-67Nqr8PSYg");
    System.out.println(decoded.getPayload());
}

    等待两秒之后在执行,你会发现它抛出如下的错误信息:

Exception in thread "main" com.auth0.jwt.exceptions.TokenExpiredException: The Token has expired on Sun Jan 17 19:45:13 CST 2021.
  at com.auth0.jwt.JWTVerifier.assertDateIsFuture(JWTVerifier.java:403)
  at com.auth0.jwt.JWTVerifier.assertValidDateClaim(JWTVerifier.java:394)
  at com.auth0.jwt.JWTVerifier.verifyClaimValues(JWTVerifier.java:314)
  at com.auth0.jwt.JWTVerifier.verifyClaims(JWTVerifier.java:303)
  at com.auth0.jwt.JWTVerifier.verify(JWTVerifier.java:288)
  at com.auth0.jwt.JWTVerifier.verify(JWTVerifier.java:271)
  at com.monkeybrother.jwt.utils.JwtUtil.main(JwtUtil.java:65)

    通过查看JWT的源码可以发现,JWT给我们提供了5中token校验的时候会抛出来的异常信息:
在这里插入图片描述
    我们知道token保存在客户端,相比于传统的token过期删除token,JWT的过期时间是怎么控制的呢?它是将时间设置为一个"claim"然后在解析的时候解析这个“claim”通过拿到字段和当前时间进行比较。

    程序报异常是不够友好的,我们可以对异常进行捕获,然后自定义我们捕获到的异常信息。于是我们的校验token的方法可以进行更改成如下:

public static void main(String[] args) {
    try {
        Algorithm algorithm = Algorithm.HMAC256("this is your secret");
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT decoded = verifier.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi5bCP54y05a2QIiwiZXhwIjoxNjEwODgzOTEzfQ.sAqi1xhajwKCxuv5sx8EwlcpbflsJs-k-67Nqr8PSYg");
    } catch (AlgorithmMismatchException e) {
        System.out.println("token算法不一致");
    } catch (InvalidClaimException e) {
        System.out.println("无效的token声明");
    } catch (JWTDecodeException e) {
        System.out.println("token解码异常");
    } catch (SignatureVerificationException e) {
        System.out.println("token签名无效");
    } catch (TokenExpiredException e) {
        System.out.println("token已过期");
    } catch (Exception e) {
        System.out.println("其他异常");
    }
}

    如果token校验合法,我们就可以从token中获取我们之前设置的“claim”:


DecodedJWT decodedJWT = JWT.require(algorithm).build().verify(token);
public static void main(String[] args) {
    Algorithm algorithm = Algorithm.HMAC256("this is your secret");
    JWTCreator.Builder builder = JWT.create().withClaim("name", "小猴子");
    String token = builder.withExpiresAt(new Date(System.currentTimeMillis() + 2000000)).sign(algorithm);
    DecodedJWT decodedJWT = JWT.require(algorithm).build().verify(token);
    System.out.println(decodedJWT.getClaim("name").asString());
}

    对于我们想要设置的“claim”总不能一个一个的添加吧,我们可以封装一个方法对我们想要添加的信息进行一次批量的添加。我这里给简单封装了一个工具类:

**
 * @Desc jwt工具类
 * @Author houry
 * @Date 2021/1/5 10:09
 **/
public class JwtUtil {
    /**
     * 秘钥:需要妥善保存,一旦泄露token有可能被破解
     */
    private static final String SECRET = "#$@!FX()!JF%JS$KLS*KFH##%#QPCIXMSJ";
    /**
     * 签名算法:推荐使用HMAC256
     */
    private static final Algorithm ALGORITHM = Algorithm.HMAC256(SECRET);

    /**
     * 设置默认过期时间 1000*60*60=3600000=1h
     */
    private static final Long EXPIRE_DATE = 3600000L;

    /**
     * 获取token信息
     *
     * @param claimMap 自定义的条件
     * @return token令牌
     */
    public static String getToken(Map<String, String> claimMap) {
        return getToken(claimMap, EXPIRE_DATE);
    }

    /**
     * @param claimMap 自定义条件
     * @param expire   过期时间 单位:毫秒
     * @return token令牌
     */
    public static String getToken(Map<String, String> claimMap, Long expire) {
        JWTCreator.Builder builder = JWT.create();
        claimMap.forEach(builder::withClaim);
        builder.withExpiresAt(new Date(System.currentTimeMillis() + expire));
        return builder.sign(ALGORITHM);
    }

    /**
     * 获取DecodedJWT
     *
     * @param token token令牌
     * @return DecodedJWT
     */
    public static DecodedJWT getDecodedJWT(String token) {
        return JWT.require(ALGORITHM).build().verify(token);
    }

    /**
     * 获取DecodedJWT
     *
     * @param algorithm 指定算法
     * @param token     token令牌
     * @return DecodedJWT
     */
    public static DecodedJWT getDecodedJWT(Algorithm algorithm, String token) {
        return JWT.require(algorithm).build().verify(token);
    }
}

    注意:这里仅仅是简简单单的介绍了如何生成token,用户可以自定义一个拦截器,对token进行拦截然后判断。对于每个用户的加密的算法,可以结合数据库,存储在数据库中这样每个人的加密算法就都不一样,不用担心一个token被破解所有的账户都会被破解。代码我已经开源,欢迎大家进行工具的补充和指正,后期我会结合SpringSecurity进行整合。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值