JwtUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package space.anyi.BI.util;
  2. import io.jsonwebtoken.Claims;
  3. import io.jsonwebtoken.JwtBuilder;
  4. import io.jsonwebtoken.Jwts;
  5. import io.jsonwebtoken.SignatureAlgorithm;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.util.Base64;
  9. import java.util.Date;
  10. import java.util.UUID;
  11. /**
  12. * @ProjectName: BI
  13. * @FileName: JwtUtil
  14. * @Author: 杨逸
  15. * @Data:2024/11/28 20:08
  16. * @Description: TODO
  17. */
  18. public class JwtUtil{
  19. //有效期为
  20. public static final Long JWT_TTL = 24*60 * 60 *1000L;// 60 * 60 *1000 一个小时
  21. //设置秘钥明文
  22. public static final String JWT_KEY = "yangyi";
  23. public static String getUUID(){
  24. String token = UUID.randomUUID().toString().replaceAll("-", "");
  25. return token;
  26. }
  27. /**
  28. * 生成jtw
  29. * @param subject token中要存放的数据(json格式)
  30. * @return
  31. */
  32. public static String createJWT(String subject) {
  33. JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
  34. return builder.compact();
  35. }
  36. /**
  37. * 生成jtw
  38. * @param subject token中要存放的数据(json格式)
  39. * @param ttlMillis token超时时间
  40. * @return
  41. */
  42. public static String createJWT(String subject, Long ttlMillis) {
  43. JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());// 设置过期时间
  44. return builder.compact();
  45. }
  46. private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
  47. SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
  48. SecretKey secretKey = generalKey();
  49. long nowMillis = System.currentTimeMillis();
  50. Date now = new Date(nowMillis);
  51. if(ttlMillis==null){
  52. ttlMillis=JwtUtil.JWT_TTL;
  53. }
  54. long expMillis = nowMillis + ttlMillis;
  55. Date expDate = new Date(expMillis);
  56. return Jwts.builder()
  57. .setId(uuid) //唯一的ID
  58. .setSubject(subject) // 主题 可以是JSON数据
  59. .setIssuer("杨逸") // 签发者
  60. .setIssuedAt(now) // 签发时间
  61. .signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥
  62. .setExpiration(expDate);
  63. }
  64. /**
  65. * 创建token
  66. * @param id
  67. * @param subject
  68. * @param ttlMillis
  69. * @return
  70. */
  71. public static String createJWT(String id, String subject, Long ttlMillis) {
  72. JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);// 设置过期时间
  73. return builder.compact();
  74. }
  75. /**
  76. * 生成加密后的秘钥 secretKey
  77. * @return
  78. */
  79. public static SecretKey generalKey() {
  80. byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
  81. SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
  82. return key;
  83. }
  84. /**
  85. * 解析
  86. *
  87. * @param jwt
  88. * @return
  89. * @throws Exception
  90. */
  91. public static Claims parseJWT(String jwt) throws Exception {
  92. SecretKey secretKey = generalKey();
  93. return Jwts.parser()
  94. .setSigningKey(secretKey)
  95. .parseClaimsJws(jwt)
  96. .getBody();
  97. }
  98. }