Springboot如何整合JwtHelper实现非对称加密
作者:WeekendFlower
时间:2023-05-13
来源:互联网
浏览:0
一、生成公私钥对提供两种方法,一种基于命令行中的Keytool工具生成,一种是基于SpringSecurity中的KeyPairGenerator类生成,现实现第二种方式://加密算法privatestaticfinalStringKEY_ALGORITHM="RSA";//公钥keyprivatestaticfinalStringPUB_KEY="publicKey";//私钥keyprivatestaticfinalStringPRI_KEY="privateKey";publicstaticMapg
一、生成公私钥对
提供两种方法,一种基于命令行中的Keytool工具生成,一种是基于SpringSecurity中的KeyPairGenerator类生成,现实现第二种方式:
// 加密算法 private static final String KEY_ALGORITHM = "RSA"; // 公钥key private static final String PUB_KEY="publicKey"; // 私钥key private static final String PRI_KEY="privateKey"; public static MapgenerateKey() throws NoSuchAlgorithmException { Map keyMap=new HashMap<>(); KeyPairGenerator instance = KeyPairGenerator.getInstance(KEY_ALGORITHM); KeyPair keyPair = instance.generateKeyPair(); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); //Base64 编码 byte[] privateKeyEncoded = privateKey.getEncoded(); String privateKeyStr = Base64.encodeBase64String(privateKeyEncoded); byte[] publicKeyEncoded = publicKey.getEncoded(); String publicKeyStr=Base64.encodeBase64String(publicKeyEncoded); keyMap.put(PUB_KEY,publicKeyStr); keyMap.put(PRI_KEY,privateKeyStr); return keyMap; }
二、利用私钥生产token
// 加密算法 private static final String KEY_ALGORITHM = "RSA"; // 公钥key private static final String PUB_KEY="publicKey"; // 私钥key private static final String PRI_KEY="privateKey"; // GenerateKey Key=new GenerateKey(); // 利用私钥生产token public static MapgenerateToken(UserDetails userDetails) throws NoSuchAlgorithmException, InvalidKeySpecException { GenerateKey Key=new GenerateKey(); RSAPrivateKey privateKey = null; RSAPublicKey publicKey=null; String token=null; Map map=new HashMap<>(); Map keyMap = Key.generateKey(); privateKey=getPrivateKey(keyMap.get(PRI_KEY)); Map tokenMap=new HashMap<>(); tokenMap.put("userName",userDetails.getUsername()); // 使用私钥加密 token = JwtHelper.encode(JSON.toJSONString(tokenMap), new RsaSigner(privateKey)).getEncoded(); map.put("token",token); map.put("publicKey",keyMap.get(PUB_KEY)); return map; }
三、利用公钥解密token
public static String parseToken(String token,String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
Jwt jwt=null;
RSAPublicKey rsaPublicKey;
rsaPublicKey=getPublicKey(publicKey);
jwt=JwtHelper.decodeAndVerify(token, new RsaVerifier(rsaPublicKey) );
String claims= jwt.getClaims();
return claims;
}四、将String类型的公钥转换成RSAPublicKey对象
/**
* 得到公钥
*
* @param publicKey
* 密钥字符串(经过base64编码)
* @throws Exception
*/
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
// 通过X509编码的Key指令获得公钥对象
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
return key;
}五、将String类型的私钥转换成RSAPrivateKey对象
/**
* 得到私钥pkcs8
*
* @param privateKey
* 密钥字符串(经过base64编码)
* @throws Exception
*/
public static RSAPrivateKey getPrivateKey(String privateKey)
throws NoSuchAlgorithmException, InvalidKeySpecException {
// 通过PKCS#8编码的Key指令获得私钥对象
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
return key;
}
作者最新文章
赤友清理大师
2026-09-16 17:43
南邮光擎智算团队:GaN基Micro-LED光计算芯片从理论到流片的突破
2026-09-08 18:35
多张照片怎么合成PDF文件?三种图片转PDF工具怎么选?
2026-09-03 17:04
Excel转PDF防乱版指南:在线与本地双方案及排版检查
2026-09-03 10:04
多个PDF怎么合并成一个?合并后顺序怎么检查?
2026-09-02 19:54
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















