当前位置:   article > 正文

Springboot - druid 数据库密码加密_configtools加密

configtools加密

Druid的ConfigTools工具类使用了RSA非对称加密算法来进行密码加密。它通过生成公钥和私钥对来进行加密和解密操作。具体的加密原理如下:

1、通过ConfigTools会生成一对RSA公钥和私钥。
  1. static {
  2. try {
  3. String[] keyPair = ConfigTools.genKeyPair(512);
  4. privateKey = keyPair[0];
  5. System.out.println("privateKey:" + privateKey);
  6. publicKey = keyPair[1];
  7. System.out.println("publicKey:" + publicKey);
  8. } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
  9. e.printStackTrace();
  10. }
  11. }

2、原始密码会使用私钥进行加密,生成密文。ConfigTools.encrypt(String key, String plainText)
  1. public static String encrypt(String plainText) throws Exception {
  2. String encrypt = ConfigTools.encrypt(privateKey, plainText);
  3. System.out.println("encrypt:" + encrypt);
  4. return encrypt;
  5. }
3、原始密码会使用公钥进行解密,生成明文。ConfigTools.decrypt(String key, String encryptText)
  1. public static String decrypt(String encryptText) throws Exception {
  2. String decrypt = ConfigTools.decrypt(publicKey, encryptText);
  3. System.out.println("decrypt:" + decrypt);
  4. return decrypt;
  5. }
4、示例如下:

5、密文保存在配置文件进行解密。

        说明:durid配置配置以后会自动进行解密,原理就是通过2中所示的方法进行解密。

6、配置文件
  1. spring:
  2. datasource:
  3. username: root
  4. password: b6/R50FChU0r52ry8ioP0NJN59WOvPi3Hm0sIvdvL6jbXshPoXTYA2AIKledgApHNSbn7XaUOrMZ1bpsQwaC3g== #加密后的密码
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/jrlyt_sub?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. druid:
  9. connectionProperties: config.decrypt=true;config.decrypt.key=${publicKey}; #公钥自动扫描
  10. filter:
  11. config:
  12. enabled: true #开启属性是扫描
  13. #加密公钥
  14. publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ2mWVq2XZ1IKrtlPwHiTITWZ5vOS0DkTxwPJydxARhR94LAU4K4nUs7LclI9Pa+gBiewC/Wp2DHj4c8Lsdq9vUCAwEAAQ==

注:别忘记导入依赖哦。

感谢小伙伴的观看,希望对你有帮助!

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

闽ICP备14008679号