当前位置: 首页 > news >正文

jwt使用rsa加密时报错(main方法测试始终正确,一使用web运行就报错)

错误信息

java.security.InvalidKeyException: IOException: DerInputStream.getLength(): lengthTag=13, too big.

原因:多线程引起的并发问题(更具体的原因不清楚)。

解决:使用synchronized (或者Lock,我懒得用Lock了)

	/**
	 * 从文件中获取公钥
	 * 
	 * @param publicKeyFilePath
	 *            文件路径
	 * @return
	 */
	public synchronized static PublicKey getPublicKey(String publicKeyFilePath) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		return getPublicKey(new FileInputStream(publicKeyFilePath));
	}

	/**
	 * 从文件中获取公钥
	 * 
	 * @param publicKeyFilePath
	 *            classpath中的文件相对路径(只有文件名都行,ClassLoader.getResourceAsStream会自行搜索)
	 * @return
	 */
	public synchronized static PublicKey getPublicKeyClasspath(String publicKeyFilePath) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		return getPublicKey(RsaKeyUtil.class.getClassLoader().getResourceAsStream(publicKeyFilePath));
	}

	/**
	 * 从字节输入流中获取公钥
	 * 
	 * @param publicKeyInput
	 * @return
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public synchronized static PublicKey getPublicKey(InputStream publicKeyInput) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		try (InputStream input = publicKeyInput; DataInputStream dis = new DataInputStream(input)) {
			byte[] keyBytes = new byte[input.available()];
			dis.readFully(keyBytes);
			X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
			KeyFactory kf = KeyFactory.getInstance("RSA");
			return kf.generatePublic(spec);
		}
	}

	public synchronized static PublicKey getPublicKey64(String publicKeyFilePath) throws Exception {
		return getPublicKey64(new FileInputStream(publicKeyFilePath));
	}

	public synchronized static PublicKey getPublicKey64Classpath(String publicKeyFilePath) throws Exception {
		return getPublicKey64(RsaKeyUtil.class.getClassLoader().getResourceAsStream(publicKeyFilePath));
	}

	public synchronized static PublicKey getPublicKey64(InputStream publicKeyInput) throws Exception {
		try (InputStream input = publicKeyInput) {
			String key64 = FileUtil.readAllString(input);
			byte[] keyByte = Base64Util.decodeBase64(key64);
			X509EncodedKeySpec spec = new X509EncodedKeySpec(keyByte);
			KeyFactory kf = KeyFactory.getInstance("RSA");
			return kf.generatePublic(spec);
		}
	}

相关文章:

  • 香港服务器不稳定的几种情况
  • windows server 2016调优
  • Vue3 实现 clipboard 复制功能
  • C++ 程序员入门需要多久,怎样才能学好?
  • 基于Qt Designer 操作教程
  • Leetcode—111.二叉树的最小深度【简单】
  • 【设计模式】第22节:行为型模式之“状态模式”
  • MySQL user权限表详解
  • Vue使用Object.definedproperty的数据监听 使用js实现一种发布订阅的模式
  • 领星ERP如何无需API开发轻松连接OA、电商、营销、CRM、用户运营、推广、客服等近千款系统
  • 华锐技术何志东:证券核心交易系统分布式改造将迎来规模化落地阶段
  • docker部署Jenkins(Jenkins+Gitlab+Maven实现CI/CD)
  • 基于MIMO通信系统的球形译码算法matlab性能仿真,对比PSK检测,SDR检测
  • 【文生图】Stable Diffusion XL 1.0模型Full Fine-tuning指南(U-Net全参微调)
  • 一文带你轻松拿下Java中的抽象类
  • 目标检测:Proposal-Contrastive Pretraining for Object Detection from Fewer Data
  • 家庭用洗地机哪个最好?家用洗地机选购
  • SPSS游程检验
  • AHD摄像头和普通摄像头
  • 【地理位置识别】IP归属地应用的特点
  • 葡萄牙三年第三次大选:“三分天下”已定,“欧洲例外”不再
  • 《远山淡影》改编电影入围戛纳关注单元,张怡微谈石黑一雄
  • 马斯克:大幅削减政治支出,仍将执掌特斯拉至少5年,除非去世
  • 上海地铁:一孩童鞋子卡于电梯梯级处,其间未造成人员受伤
  • 西安集中整治监督教育领域不正之风和腐败问题,举报方式公布
  • 英国研究:近七成年轻人认为上网有害心理健康