当前位置:   article > 正文

SpringBoot Druid对配置文件中数据库密码加密_druid的配置文件

druid的配置文件

引用依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid-spring-boot-starter</artifactId>
  4. <version>1.2.16</version>
  5. </dependency>

配置文件,其中password为加密过的密码

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
  6. username: root
  7. password: gIAJRNPtYE2n+00XbtYHUZiXp3wJXkBgavDauejensplt4js+wVnl/kS7bPBra8YdBtxc4WgtVcnbZvskeOofQ==
  8. druid:
  9. initial-size: 10
  10. max-active: 100
  11. min-idle: 10
  12. max-wait: 6000
  13. pool-prepared-statements: true
  14. max-pool-prepared-statement-per-connection-size: 20
  15. time-between-eviction-runs-millis: 60000
  16. min-evictable-idle-time-millis: 300000
  17. #Oracle需要打开注释
  18. # validation-query: SELECT 1 FROM DUAL
  19. test-while-idle: true
  20. test-on-borrow: false
  21. test-on-return: false
  22. stat-view-servlet:
  23. enabled: true
  24. url-pattern: /druid/*
  25. login-username: admin
  26. login-password: 123456
  27. filter:
  28. stat:
  29. log-slow-sql: true
  30. slow-sql-millis: 1000
  31. merge-sql: false
  32. wall:
  33. config:
  34. multi-statement-allow: true
  35. config:
  36. enabled: true

DataSource配置类,其中PUBLICKEY公钥为生成的用于解析加密密码所用

  1. @Configuration
  2. public class DataSourceConfig {
  3. private static final String PUBLICKEY="MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI" +
  4. "+pQptB9iOABJIJWoFeVkMQ7B26fSggMY6JUZLpcaNng/SKN70EKcadOxlF1D2hf27ZTRw/E9zG4DUZZGLRIT0CAwEAAQ==";
  5. @Value("${spring.datasource.url}")
  6. private String url;
  7. @Value("${spring.datasource.username}")
  8. private String username;
  9. @Value("${spring.datasource.password}")
  10. private String password;
  11. @Value("${spring.datasource.driver-class-name}")
  12. private String driverClass;
  13. @Bean
  14. @Primary
  15. public DataSource dataSource() throws SQLException {
  16. DruidDataSource dataSource = new DruidDataSource();
  17. dataSource.setDriverClassName(this.driverClass);
  18. dataSource.setUrl(this.url);
  19. dataSource.setUsername(this.username);
  20. dataSource.setPassword(this.password);
  21. Properties properties = new Properties();
  22. properties.setProperty("config.decrypt", "true");
  23. properties.setProperty("config.decrypt.key", PUBLICKEY);
  24. dataSource.setConnectProperties(properties);
  25. dataSource.setFilters("config");
  26. return dataSource;
  27. }
  28. }

 公私钥的生成,Druid加密密码原理,私钥+明文密码生成加密后密码,公钥+加密后密码生成解密后明文密码

  1. String[] keyPair = ConfigTools.genKeyPair(512);
  2. String privateKey = keyPair[0];
  3. System.out.println(String.format("privateKey-->%s", privateKey));
  4. String publicKey = keyPair[1];
  5. System.out.println(String.format("publicKey-->%s", publicKey));
  6. String plainText = "123456";
  7. System.out.println("明文密码:" + plainText);
  8. String cipherText = ConfigTools.encrypt(privateKey, plainText);
  9. System.out.println("加密后密码:" + cipherText);
  10. System.out.println("加密密码:" + cipherText);
  11. String plainTextnew = ConfigTools.decrypt(publicKey, cipherText);
  12. System.out.println("解密后密码:" + plainTextnew);

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

闽ICP备14008679号