这篇文章主要介绍了SpringBoot如何整合JwtHelper实现非对称加密,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、生成公私钥对提供两种方法,一种基于命令行中的K
这篇文章主要介绍了SpringBoot如何整合JwtHelper实现非对称加密,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
提供两种方法,一种基于命令行中的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 Map<String,String> generateKey() throws NoSuchAlgorithmException { Map<String,String> 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; }
// 加密算法 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 Map<String,String> generateToken(UserDetails userDetails) throws NoSuchAlgorithmException, InvalidKeySpecException { GenerateKey Key=new GenerateKey(); RSAPrivateKey privateKey = null; RSAPublicKey publicKey=null; String token=null; Map<String, String> map=new HashMap<>(); Map<String, String> keyMap = Key.generateKey(); privateKey=getPrivateKey(keyMap.get(PRI_KEY)); Map<String,String> 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; }
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; }
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; }
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; }
感谢你能够认真阅读完这篇文章,希望小编分享的“Springboot如何整合JwtHelper实现非对称加密”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: Springboot如何整合JwtHelper实现非对称加密
本文链接: https://www.lsjlt.com/news/326081.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0