Springboot整合JwtHelper實現(xiàn)非對稱加密
更新時間:2022年03月31日 12:14:31 作者:Love is beautiful
本文主要介紹了Springboot整合JwtHelper實現(xiàn)非對稱加密,主要介紹兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
一、生成公私鑰對
提供兩種方法,一種基于命令行中的Keytool工具生成,一種是基于SpringSecurity中的KeyPairGenerator類生成,現(xiàn)實現(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對象
/** * 得到公鑰 * * @param publicKey * 密鑰字符串(經(jīng)過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類型的私鑰轉(zhuǎn)換成RSAPrivateKey對象
/** * 得到私鑰pkcs8 * * @param privateKey * 密鑰字符串(經(jīng)過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; }
到此這篇關(guān)于Springboot整合JwtHelper實現(xiàn)非對稱加密的文章就介紹到這了,更多相關(guān)Springboot JwtHelper非對稱加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC 通過commons-fileupload實現(xiàn)文件上傳功能
這篇文章主要介紹了SpringMVC 通過commons-fileupload實現(xiàn)文件上傳,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Java內(nèi)存模型之重排序的相關(guān)知識總結(jié)
重排序是指編譯器和處理器為了優(yōu)化性能而對指令序列進(jìn)行重新排序的一種手段,文中詳細(xì)介紹了Java重排序的相關(guān)知識,需要的朋友可以參考下2021-06-06