您好,登錄后才能下訂單哦!
這篇文章主要介紹了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; }
/** * 得到公鑰 * * @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; }
/** * 得到私鑰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; }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Springboot如何整合JwtHelper實現非對稱加密”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。