手写一个Java的JWT工具类

// JWT工具类
public class JwtUtil {

    // 默认有效期 1天
    public static final Long JWT_TTL = 1000L * 60 * 60 * 24;
    // 密钥
    public static final String JWT_KEY = "root";

    /**
     * 核心的创建jwt的方法
     *
     * @param subject   主题
     * @param ttlMillis 过期时间
     * @param uuid      用户id
     * @return
     */
    private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
        // 加密方式
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 密钥
        SecretKey secretKey = generalKey();
        // 当前时间
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        // 过期时间
        if (ttlMillis == null) ttlMillis = JwtUtil.JWT_TTL;
        long expMillis = nowMillis + ttlMillis;
        Date expDate = new Date(expMillis);

        return Jwts.builder()
                .setId(uuid) //唯一的ID
                .setSubject(subject) // 主题 可以是JSON数据
                // .setIssuer("sg") // 签发者
                .setIssuedAt(now) // 签发时间
                .setExpiration(expDate) // 过期时间
                .signWith(signatureAlgorithm, secretKey); //使用HS256对称加密算法签名, 第二个参数为密钥
    }

    /**
     * 生成jwt 重载1
     *
     * @param subject token中要存放的数据(json格式)
     * @return
     */
    public static String createJWT(String subject) {
        JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
        return builder.compact();
    }

    /**
     * 生成jwt 重载2
     *
     * @param subject   token中要存放的数据(json格式)
     * @param ttlMillis token超时时间
     * @return
     */
    public static String createJWT(String subject, Long ttlMillis) {
        JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());// 设置过期时间
        return builder.compact();
    }
    
    // 生成jwt 重载3
    public static String createJWT(String id, String subject, Long ttlMillis) {
        JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);// 设置过期时间
        return builder.compact();
    }

    // 对密钥二次加密
    public static SecretKey generalKey() {
        byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
        SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
        return key;
    }

    // 解密
    public static Claims parseJWT(String jwt) throws Exception {
        return Jwts.parser()
                // 密钥
                .setSigningKey(generalKey())
                // 传入的jwt
                .parseClaimsJws(jwt)
                .getBody();
    }

    public static String getUUID() {
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        return token;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋劲豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值