JJWT是一个提供端到端的JWT创建和验证的Java库,永远免费和开源(Apache License,版本2.0)。JJW很容易使用和理解。它被设计成一个以建筑为中心的流畅界面,隐藏了它的大部分复杂性。

快速开始

引入依赖

  1. <!--JWT依赖-->
  2. <dependency>
  3. <groupId>io.jsonwebtoken</groupId>
  4. <artifactId>jjwt</artifactId>
  5. <version>0.9.1</version>
  6. </dependency>

创建token

  1. public class JwtdemoApplicationTests {
  2. private static final String SECRETKEY="123123";
  3. @Test
  4. public void test() {
  5. //创建一个JwtBuilder对象
  6. JwtBuilder jwtBuilder = Jwts.builder()
  7. //声明的标识{"jti":"666"}
  8. .setId("666")
  9. //主体,用户{"sub":"Fox"}
  10. .setSubject("Fox")
  11. //创建日期{"ita":"xxxxxx"}
  12. .setIssuedAt(new Date())
  13. //设置过期时间 1分钟
  14. .setExpiration(new Date(System.currentTimeMillis()+60*1000))
  15. //claim可以存自己的一些信息
  16. //甚至可以直接传入map
  17. // .addClaims(map)
  18. .claim("roles","admin")
  19. .claim("logo","xxx.jpg")
  20. //签名手段,参数1:算法,参数2:盐
  21. .signWith(SignatureAlgorithm.HS256, SECRETKEY);
  22. //获取token jwt
  23. String token = jwtBuilder.compact();
  24. System.out.println(token);
  25. // eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjYiLCJzdWIiOiJGb3giLCJpYXQiOjE2NDQyODY3MTYsImV4cCI6MTY0NDI4Njc3Niwicm9sZXMiOiJhZG1pbiIsImxvZ28iOiJ4eHguanBnIn0.dixD_GK5eYBXKdQDTQnwxn3i5TSoTp4SJvXKoqjdNVY
  26. //三部分的base64解密
  27. System.out.println("=========");
  28. String[] split = token.split("\\.");
  29. System.out.println(Base64Codec.BASE64.decodeToString(split[0]));
  30. // {"alg":"HS256"}
  31. System.out.println(Base64Codec.BASE64.decodeToString(split[1]));
  32. // {"jti":"666","sub":"Fox","iat":1644286716,"exp":1644286776,"roles":"admin","logo":"xxx.jpg
  33. //base64无法解密,所以输出的是乱码,无法解析的原因是为了安全性保证
  34. System.out.println(Base64Codec.BASE64.decodeToString(split[2]));
  35. // v,C�^`�u �B|1�x�M*����r��7M
  36. }
  37. }

输出结果:

  1. eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjYiLCJzdWIiOiJGb3giLCJpYXQiOjE2NDQyODc0NDIsImV4cCI6MTY0NDI4NzUwMiwicm9sZXMiOiJhZG1pbiIsImxvZ28iOiJ4eHguanBnIn0.veExdFdoZpHSXLSs1leQDPHx0dHt-ILLfDeilWucf7U
  2. =========
  3. {"alg":"HS256"}
  4. {"jti":"666","sub":"Fox","iat":1644287442,"exp":1644287502,"roles":"admin","logo":"xxx.jpg
  5. Z�

token的验证解析

  1. public class JwtdemoApplicationTests {
  2. private static final String SECRETKEY="123123";
  3. @Test
  4. public void testParseToken(){
  5. //token
  6. String token ="eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjYiLCJzdWIiOiJGb3giLCJpYXQiOjE2NDQyODcwNjEsImV4cCI6MTY0NDI4NzEyMSwicm9sZXMiOiJhZG1pbiIsImxvZ28iOiJ4eHguanBnIn0.5tqXl0BvW_mUwizRTnCAZSulDEhZWypyzzvIc-o_zBM";
  7. //解析token获取载荷中的声明对象
  8. Claims claims = Jwts.parser()
  9. .setSigningKey(SECRETKEY)
  10. .parseClaimsJws(token)
  11. .getBody();
  12. //如果token过期的话,就会抛异常出来
  13. System.out.println("id:"+claims.getId());
  14. System.out.println("subject:"+claims.getSubject());
  15. System.out.println("issuedAt:"+claims.getIssuedAt());
  16. DateFormat sf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  17. System.out.println("签发时间:"+sf.format(claims.getIssuedAt()));
  18. System.out.println("过期时间:"+sf.format(claims.getExpiration()));
  19. System.out.println("当前时间:"+sf.format(new Date()));
  20. System.out.println("roles:"+claims.get("roles"));
  21. System.out.println("logo:"+claims.get("logo"));
  22. }
  23. }

如果token没过期的话输出结果:

  1. id:666
  2. subject:Fox
  3. issuedAt:Tue Feb 08 10:30:42 CST 2022
  4. 签发时间:2022-02-08 10:30:42
  5. 过期时间:2022-02-08 10:31:42
  6. 当前时间:2022-02-08 10:31:09
  7. roles:admin
  8. logo:xxx.jpg

如果token已经过期的话输出结果:

  1. io.jsonwebtoken.ExpiredJwtException: JWT expired at 2022-02-08T10:31:42Z. Current time: 2022-02-08T10:31:57Z, a difference of 15635 milliseconds. Allowed clock skew: 0 milliseconds.