Springboot整合JwtHelper實(shí)現(xiàn)非對(duì)稱加密
一、生成公私鑰對(duì)
提供兩種方法,一種基于命令行中的Keytool工具生成,一種是基于SpringSecurity中的KeyPairGenerator類生成,現(xiàn)實(shí)現(xiàn)第二種方式:
// 加密算法
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;
}
二、利用私鑰生產(chǎn)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();
// 利用私鑰生產(chǎn)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;
}
三、利用公鑰解密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類型的公鑰轉(zhuǎn)換成RSAPublicKey對(duì)象
/**
* 得到公鑰
*
* @param publicKey
* 密鑰字符串(經(jīng)過base64編碼)
* @throws Exception
*/
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
// 通過X509編碼的Key指令獲得公鑰對(duì)象
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
return key;
}
五、將String類型的私鑰轉(zhuǎn)換成RSAPrivateKey對(duì)象
/**
* 得到私鑰pkcs8
*
* @param privateKey
* 密鑰字符串(經(jīng)過base64編碼)
* @throws Exception
*/
public static RSAPrivateKey getPrivateKey(String privateKey)
throws NoSuchAlgorithmException, InvalidKeySpecException {
// 通過PKCS#8編碼的Key指令獲得私鑰對(duì)象
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
return key;
}
到此這篇關(guān)于Springboot整合JwtHelper實(shí)現(xiàn)非對(duì)稱加密的文章就介紹到這了,更多相關(guān)Springboot JwtHelper非對(duì)稱加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何通過properties文件配置web.xml中的參數(shù)
這篇文章主要介紹了如何通過properties文件配置web.xml中的參數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java實(shí)現(xiàn)八種排序算法詳細(xì)代碼舉例
排序問題一直是程序員工作與面試的重點(diǎn),今天特意整理研究下與大家共勉!這篇文章主要介紹了Java實(shí)現(xiàn)八種排序算法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
Java設(shè)計(jì)模式之動(dòng)態(tài)代理
今天小編就為大家分享一篇關(guān)于Java設(shè)計(jì)模式之動(dòng)態(tài)代理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
Spring?AOP?創(chuàng)建代理對(duì)象詳情
這篇文章介紹了Spring?AOP?創(chuàng)建代理對(duì)象詳情,主要介紹AOP?創(chuàng)建代理對(duì)象和上下文相關(guān)的內(nèi)容,下文分享具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
聊聊SpringBoot的@Scheduled的并發(fā)問題
這篇文章主要介紹了聊聊SpringBoot的@Scheduled的并發(fā)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Jedis出現(xiàn)connection timeout問題解決方法(JedisPool連接池使用實(shí)例)
這篇文章主要介紹了Jedis出現(xiàn)connection timeout問題解決方法,使用Jedis的JedisPool連接池解決了這個(gè)問題,需要的朋友可以參考下2014-05-05
Java連接mysql數(shù)據(jù)庫代碼實(shí)例程序
這篇文章主要介紹了java連接mysql數(shù)據(jù)庫代碼實(shí)例程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

