1.数据库密码加密:
1.maven引入druid jar包
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
2.打开命令行,进入到druid 的目录,如C:\Users\chgh.m2\repository\com\alibaba\druid\1.1.21>
执行指令:java -cp druid-1.1.21.jar com.alibaba.druid.filter.config.ConfigTools 你的密码(示例为123456):
privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA0Fu+pHGa4WkEN57Jcgl65FZ+VTRVyK6ElbsC1+VRIy5ihGcSVqN2FS4dI5AQlGRPV0MskQj+n/MoTV0pHKyqawIDAQABAkEAjrXIJioJw0qCsPX5VDpaCfuUHBf0ibe+6C9nCyjuIgFu5+vGaLfpmR7MajoWwlomw9ZBx4QyVmq3foCSIPxxwQIhAOegxR7AOa6YWt9X073FOfd7GPYKwgc9y2IauroUDpNnAiEA5kgskZsTlL9AniY3xzfSvMsSl88bEJlVF2caPZFRMl0CIQCtu4SeoAej0Vq3kqSa/keQPKBoS4Fk9RCLsQjFcS3NmQIhAKQJbZYa/YvL4N/HIZyLq1ZI4em54fGlB/QvwYVx6oERAiAlF2ArHllv+Q5YfqLdgiEpT+eRkkimdvxlpRtjEI2b3w==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANBbvqRxmuFpBDeeyXIJeuRWflU0VciuhJW7AtflUSMuYoRnElajdhUuHSOQEJRkT1dDLJEI/p/zKE1dKRysqmsCAwEAAQ==
password:e2iMnDbFDfvIYj+QMbV7x0fuyjcTG6cwCtAXzq2DwDOIHo91IqBjnU9qB89FFf9X5AAJqnFaoRWGhe7qhmYucQ==
3.此时获取到公钥publicKey,私钥privateKey,以及密码password,取publicKey及password填入druid的配置中即可
# mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
# 生成的加密后的密码(原密码 123456)
spring.datasource.password=WVMjPhfXQrIsWRo0/RCqAVvYtTU9WNVToKJohb8AlUmHwnV6vwFL+FM2CNFDMJwGHW1iCmyaUlF+sgvFdogqEA==
# 生成的公钥
public-key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIiwHpFrDijV+GzwRTzWJk8D3j3jFfhsMFJ/7k1NTvBuLgL+TdIHgaMNOIEjHpXzuvX38J3FtOK8hLrySncVGOMCAwEAAQ==
# 配置 connection-properties,启用加密,配置公钥。
spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${public-key}
# 启用ConfigFilter
spring.datasource.druid.filter.config.enabled=true
2.数据库密码解密:
得到加密后的密码,和publicKey,利用druid包里的ConfigTools的decrypt方法解密:
import com.alibaba.druid.filter.config.ConfigTools;
public class DruidUtil {
public static void main(String[] args) throws Exception {
String password = "123456";
System.out.println(ConfigTools.encrypt(password));
}