使用JWT完成非对称加密代码演示

首先我们导入两个相关依赖

<!-- hutool-crypto -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-crypto</artifactId>
    <version>5.7.16</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>4.4.0</version>
</dependency>

然后下面是非对称加密的示例代码和非对称加密的示例代码,并提供main函数调用示例

package com.example.cybg.utils;

import cn.hutool.crypto.asymmetric.RSA;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.cybg.entity.User;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 加密解密token
 */
public class JWTAuth0Util {

    private static final String SECRET = "H2OJ20FC35APACHE";

    //私钥和公钥
    private static final String PRIVATE_KEY_STR = "私钥";
    private static final String PUBLIC_KEY_STR = "公钥";

    /**
     * 根据RSA算法加密生成token【非对称】
     * @param payload
     * @return
     */
    public static String genTokenRAS(Map<String, String> payload){
        //指定过期时间 【3天-->6小时】
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 6);

        //获取一个构建对象【静态内部类】
        JWTCreator.Builder builder = JWT.create();

        //将信息、数据(body)信息放到需要的claim里面
        payload.forEach((k, v) -> builder.withClaim(k, v));

        //通过hutool工具类来创建RSA对象
        RSA rsa = new RSA(PRIVATE_KEY_STR, null);

        //获取私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) rsa.getPrivateKey();

        String token = builder.withExpiresAt(calendar.getTime()).sign(Algorithm.RSA256(null, privateKey));
        return token;
    }

    /**
     * 使用RSA的方式来解密【非对称】
     * @param token
     * @return
     * 根据不同的异常来判断当前token到底是什么情况【比如被伪造...】
     */
    public static DecodedJWT decodedRSA(String token){
        //通过公钥获取我们的RSA的公钥对象
        RSA rsa = new RSA(null, PUBLIC_KEY_STR);
        RSAPublicKey publicKey = (RSAPublicKey) rsa.getPublicKey();

        //jwt的验证对象
        JWTVerifier jwtVerifier = JWT.require(Algorithm.RSA256(publicKey, null)).build();
        return jwtVerifier.verify(token);
    }


    /**
     * 获取token【对称加密】
     * @param payload
     * @return
     */
    public static String getToken(Map<String, String> payload){
        //指定过期时间【3天】
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 3);

        //获取一个构建对象【静态内部类】
        JWTCreator.Builder builder = JWT.create();

        //将body信息放到我们需要生成的claim里面
        payload.forEach((k, v) -> builder.withClaim(k, v));

        //通过指定签名算法和过期时间生成一个token
        String token = builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(SECRET));
        return token;
    }

    /**
     * 解析token【对称加密】
     * @param token
     * @return
     */
    public static DecodedJWT decodedJWT(String token){
        //构建一个验证jwt token的对象
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        //验证以后获取信息的对象
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        //返回信息对象
        return decodedJWT;
    }

    public static void main(String[] args) {
        //这里演示的是非对称加密的使用
        HashMap<String, String> map = new HashMap<>();
        map.put("username","caiyi");
        map.put("password","123456");
        map.put("method","JWT");
        String s = JWTAuth0Util.genTokenRAS(map);
        System.out.println("非对称加密后的token:" + s);

        DecodedJWT decodedJWT = JWTAuth0Util.decodedRSA(s);
        System.out.println("非对称解密后的token:" + decodedJWT);
        System.out.println("非对称解密后的具体信息:" +decodedJWT.getClaims());

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值