当前位置: 首页 > 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);
		}
	}
http://www.dtcms.com/a/3497.html

相关文章:

  • 香港服务器不稳定的几种情况
  • 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归属地应用的特点
  • 二十、泛型(1)
  • 设计交换机原理图前应先理清的框图
  • 清华大学利用可解释机器学习,优化光阳极催化剂,助力光解水制氢
  • c++中httplib使用
  • SpringBoot----自定义Start(自定义依赖)
  • 用前端框架Bootstrap的AdminLTE模板和Django实现后台首页的页面
  • Java判断是否有特殊字符串
  • PC应用管理工具 连接流程图 支持所有android手机或设备 批量设备批量应用安装卸载等管理 OS升级 push文件夹等
  • 分体式离子风刀和整体式离子风刀分别有哪些优缺点
  • Linux虚拟网络设备之tun/tap和veth设备的特点