Spring Boot整合JWT简单实现Token的验证

Spring Boot整合JWT简单实现Token的验证

导入依赖
		<!--JWT-->
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.1</version>
        </dependency>
源码
	// 解密
	public static DecodedJWT decode(String token) throws JWTDecodeException {
        return new JWTDecoder(token);
    }
	// 验证
    public static Verification require(Algorithm algorithm) {
        return JWTVerifier.init(algorithm);
    }
	// 生成
    public static Builder create() {
        return JWTCreator.init();
    }
简单实现
	/**
     * 生成token
     */
    public static String generateToken(String mobile) {
        try {
            // 加密密钥
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            // 过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRED_TIME);
            // 生成token
            return JWT
                    .create()
                    // 自定义参数
                    .withClaim("mobile", mobile)
                    // 过期时间
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (Exception e) {
            log.error("generateToken error mobile:{}", mobile);
            return null;
        }
    }

    /**
     * 校验token
     */
    public static Boolean verifyToken(String token, String mobile) {
        try {
            // 算法加密密钥
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            // 验证类
            JWTVerifier jwtVerifier = JWT
                    .require(algorithm)
                    .withClaim("mobile", mobile)
                    .build();
            jwtVerifier.verify(token);
            return true;
        } catch (Exception e) {
            log.error("verifyToken error mobile:{}", mobile);
            return false;
        }
    }

    /**
     * 解析token获取手机号
     */
    public static String getMobileByToken(String token) {
        try {
            DecodedJWT decodedJWT = JWT.decode(token);
            return decodedJWT.getClaim("mobile").asString();
        } catch (Exception e) {
            log.error("getMobileByToken error");
            return null;
        }
    }
测试
	public static void main(String[] args) {
        // test
        String mobile = "18888888888";
        String token = JWTUtil.generateToken(mobile);
        System.out.println(token);
        System.out.println(JWTUtil.verifyToken(token, mobile));
        System.out.println(JWTUtil.getMobileByToken(token));
    }

	// token
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtb2JpbGUiOiIxODg4ODg4ODg4OCIsImV4cCI6MTYzNjM2NTExOH0.IJCA5SUvOPLrr-kmn3PeEWJK32O3mvrkyav74Ef4ZPM
	// 验证
	true
	// 获取手机号
	18888888888
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值