1. import io.jsonwebtoken.Claims;
    2. import io.jsonwebtoken.JwtBuilder;
    3. import io.jsonwebtoken.Jwts;
    4. import io.jsonwebtoken.SignatureAlgorithm;
    5. import javax.crypto.SecretKey;
    6. import javax.crypto.spec.SecretKeySpec;
    7. import java.util.Base64;
    8. import java.util.Date;
    9. import java.util.UUID;
    10. /**
    11. * JWT工具类
    12. */
    13. public class JwtUtil {
    14. //有效期为
    15. public static final Long JWT_TTL = 24*60 * 60 *1000L;// 60 * 60 *1000 一个小时
    16. //设置秘钥明文
    17. public static final String JWT_KEY = "lionkliu";
    18. public static String getUUID(){
    19. String token = UUID.randomUUID().toString().replaceAll("-", "");
    20. return token;
    21. }
    22. /**
    23. * 生成jtw
    24. * @param subject token中要存放的数据(json格式)
    25. * @return
    26. */
    27. public static String createJWT(String subject) {
    28. JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
    29. return builder.compact();
    30. }
    31. /**
    32. * 生成jtw
    33. * @param subject token中要存放的数据(json格式)
    34. * @param ttlMillis token超时时间
    35. * @return
    36. */
    37. public static String createJWT(String subject, Long ttlMillis) {
    38. JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());// 设置过期时间
    39. return builder.compact();
    40. }
    41. private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
    42. SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
    43. SecretKey secretKey = generalKey();
    44. long nowMillis = System.currentTimeMillis();
    45. Date now = new Date(nowMillis);
    46. if(ttlMillis==null){
    47. ttlMillis=JwtUtil.JWT_TTL;
    48. }
    49. long expMillis = nowMillis + ttlMillis;
    50. Date expDate = new Date(expMillis);
    51. return Jwts.builder()
    52. .setId(uuid) //唯一的ID
    53. .setSubject(subject) // 主题 可以是JSON数据
    54. .setIssuer("sg") // 签发者
    55. .setIssuedAt(now) // 签发时间
    56. .signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥
    57. .setExpiration(expDate);
    58. }
    59. /**
    60. * 创建token
    61. * @param id
    62. * @param subject
    63. * @param ttlMillis
    64. * @return
    65. */
    66. public static String createJWT(String id, String subject, Long ttlMillis) {
    67. JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);// 设置过期时间
    68. return builder.compact();
    69. }
    70. public static void main(String[] args) throws Exception {
    71. String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjYWM2ZDVhZi1mNjVlLTQ0MDAtYjcxMi0zYWEwOGIyOTIwYjQiLCJzdWIiOiJzZyIsImlzcyI6InNnIiwiaWF0IjoxNjM4MTA2NzEyLCJleHAiOjE2MzgxMTAzMTJ9.JVsSbkP94wuczb4QryQbAke3ysBDIL5ou8fWsbt_ebg";
    72. Claims claims = parseJWT(token);
    73. System.out.println(claims);
    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. }