java 使用jks公钥字符串 验签jwt Token

jks中存储了一个密钥对,包括私钥和公钥.

在spring security 和 oauth2 的整合中,使用文件名为 oauth2.jks 的文件作为密钥对.

在登录时使用秘钥对的私钥加密jwt Token,那么在验证jwt时,就需要提供秘钥对的公钥,同时项目中已经存在该秘钥对的公钥,是以文件的方式存放.那么我们如何用这个公钥字符串文件来验证jwt?

private boolean verifyVisitsNumber(String value) {
        ClassPathResource resource = new ClassPathResource("public.txt");
        String publicKey = null;
        try {
            publicKey = IOUtils.toString(resource.getInputStream(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        PublicKey publicKey1=null;
        try {
            publicKey1 = pemToPublicKey(publicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Claims body = Jwts.parser()
                .setSigningKey(publicKey1)
                .parseClaimsJws(value)
                .getBody();

        Map<String,String> userInfo = (Map<String,String>)body.get("userInfo");

        String uid = userInfo.get("uid");

        if(StringUtils.equals(uid,"9")){
            //超级管理员不计次数
            return true;
        }

        //明天凌晨
        DateTime dateTime = DateUtil.beginOfDay(DateUtil.tomorrow());

        Date date = new Date();


        //距离多少分钟到凌晨->到凌晨自动重置访问次数
        long betweenMinutes = DateUtil.between(date, dateTime, DateUnit.MINUTE);

        String o = (String)redisTemplate.opsForValue().get(visitsNumberPrefix + ":" + uid);

        if(StringUtils.isBlank(o)){
            redisTemplate.opsForValue().set(visitsNumberPrefix + ":" + uid,"1",betweenMinutes, TimeUnit.MINUTES);
            o="1";
        }else{
            redisTemplate.opsForValue().set(visitsNumberPrefix + ":" + uid,String.valueOf(Integer.valueOf(o)+1),betweenMinutes, TimeUnit.MINUTES);
            o=String.valueOf(Integer.valueOf(o)+1);
        }

        if(Integer.valueOf(o) > visitsNumber){
            //该用户当前请求次数耗尽
            return false;
        }
        return true;
    }
/**
     * 将PEM格式的RSA公钥字符串转换为PublicKey对象
     *
     * @param pubKeyPEM PEM格式的RSA公钥字符串
     * @return PublicKey对象
     * @throws Exception 如果转换过程中发生错误
     */
    public static PublicKey pemToPublicKey(String pubKeyPEM) throws Exception {
        String publicKeyPEMHeader = "-----BEGIN PUBLIC KEY-----";
        String publicKeyPEMFooter = "-----END PUBLIC KEY-----";

        // 移除PEM格式的头部和尾部
        String pubKey = pubKeyPEM.replace(publicKeyPEMHeader, "").replace(publicKeyPEMFooter, "").replaceAll("\\s+", "");

        // Base64解码
        byte[] encoded = Base64.getDecoder().decode(pubKey);

        // 创建X509EncodedKeySpec
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);

        // 获取RSA密钥工厂
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        // 生成PublicKey
        return keyFactory.generatePublic(keySpec);
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值