当前位置:   article > 正文

Spring Security Oauth2密码模式 登录密码RSA加密_oauth2 登录 加密

oauth2 登录 加密

1、生成RSA非对称密钥

用了java.security自带的生成器

  1. public static void main(String[] args) throws NoSuchAlgorithmException {
  2. // 创建一个RSA密钥生成器
  3. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  4. // 设置密钥长度
  5. keyGen.initialize(2048);
  6. // 生成密钥对
  7. KeyPair keyPair = keyGen.generateKeyPair();
  8. PublicKey publicKey = keyPair.getPublic();
  9. PrivateKey privateKey = keyPair.getPrivate();
  10. // 将公钥和私钥转换为Base64编码的字符串
  11. String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
  12. String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
  13. // 打印公钥和私钥
  14. System.out.println("Public Key: " + publicKeyString);
  15. System.out.println("Private Key: " + privateKeyString);
  16. }

2、测试一把

附加解密后端util

  1. public class RsaUtil {
  2. private static final String RSA_KEY_ALGORITHM = "RSA";
  3. /**
  4. * 公钥加密(用于数据加密)
  5. *
  6. * @param data 加密前的字符串
  7. * @param publicKeyStr base64编码后的公钥
  8. * @return base64编码后的字符串
  9. * @throws Exception throw
  10. */
  11. public static String encryptByPublicKey(String data, String publicKeyStr) throws Exception {
  12. //Java原生base64解码
  13. byte[] pubKey = Base64.getDecoder().decode(publicKeyStr);
  14. //创建X509编码密钥规范
  15. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
  16. //返回转换指定算法的KeyFactory对象
  17. KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
  18. //根据X509编码密钥规范产生公钥对象
  19. PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
  20. //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式)
  21. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  22. //用公钥初始化此Cipher对象(加密模式)
  23. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  24. //对数据加密
  25. byte[] encrypt = cipher.doFinal(data.getBytes());
  26. //返回base64编码后的字符串
  27. return Base64.getEncoder().encodeToString(encrypt);
  28. }
  29. /**
  30. * 私钥解密(用于数据解密)
  31. *
  32. * @param data 解密前的字符串
  33. * @param privateKeyStr 私钥
  34. * @return 解密后的字符串
  35. * @throws Exception throw
  36. */
  37. public static String decryptByPrivateKey(String data, String privateKeyStr) throws Exception {
  38. //Java原生base64解码
  39. byte[] priKey = Base64.getDecoder().decode(privateKeyStr);
  40. //创建PKCS8编码密钥规范
  41. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey);
  42. //返回转换指定算法的KeyFactory对象
  43. KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
  44. //根据PKCS8编码密钥规范产生私钥对象
  45. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
  46. //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式)
  47. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  48. //用私钥初始化此Cipher对象(解密模式)
  49. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  50. //对数据解密
  51. byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data));
  52. //返回字符串
  53. return new String(decrypt);
  54. }
  55. }

3、修改auth模块配置

思路很清晰,原先用BcryptPasswordEncoder,那么继续用这个,只不过在这之前加上RSA解密。

3.1新建RsaBcryptPasswordEncoder

  1. @Slf4j
  2. public class RsaBcryptPasswordEncoder extends BCryptPasswordEncoder implements PasswordEncoder {
  3. private static final String RSA_KEY_ALGORITHM = "RSA";
  4. private static final String PRIVATE_KEY ="pri key";
  5. @Override
  6. public boolean matches(CharSequence rawPassword, String encodedPassword) {
  7. try {
  8. String decryptedPassword = decryptByPrivateKey(rawPassword.toString());
  9. return super.matches(decryptedPassword, encodedPassword);
  10. } catch (Exception e) {
  11. log.error(e.getMessage());
  12. return false;
  13. }
  14. }
  15. private static String decryptByPrivateKey(String data) throws Exception {
  16. byte[] priKey = Base64.getDecoder().decode(PRIVATE_KEY);
  17. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey);
  18. KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
  19. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
  20. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  21. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  22. byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data));
  23. return new String(decrypt);
  24. }
  25. }

需要在解密进行异常捕获,密文不合规之类的

3.2修改WebSecurityConfig

  1. @Bean
  2. public PasswordEncoder passwordEncoder() {
  3. return new RsaBcryptPasswordEncoder();
  4. }

注意:

在 oauth认证服务器配置 

configure(ClientDetailsServiceConfigurer clients)

方法中,也有passwordEncoder

4、测试

before

now

refresh

成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/948523
推荐阅读
相关标签
  

闽ICP备14008679号