1、在cmd控制台运行以下命令
jar下载:https://repo1.maven.org/maven2/com/alibaba/druid/1.1.21/druid-1.1.21.jar
java -cp druid-1.1.21.jar com.alibaba.druid.filter.config.ConfigTools root //root 就是你要加密的密码
执行结果:
C:administratorProgram FilesThunder Networkgv>java -cp druid-1.1.21.jar com.alibaba.druid.filter.config.ConfigTools root
privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAq8QAe5w6oE8xL/aHJLm+8EQjuL8ok1ACdwDKygDOMP3MtPTkRRhNxIz+vLomp5UHAjYHAuB7/E9Ghc7tM1/IHQIDAQABAkEAmclKOWIeZ2uf+XZ7d1lK6CnJ5d5ZANQQ8OVnq0bfVaJb8v8fJZiMEE5rnJse0CzvjxgjL2SZ0Ih7T6gV02BeLQIhAPC5alnJ8F0gEflBMtn4upWNeTjxbeWZRCpOezJrwb4PAiEAtqpZD5+7NKtCImQKPY9eGSbEAt2CM4T0ZdbDEWFzgxMCIQC70wAvJMhd8Qr1H7EMr+ztiaA/FOl5jvfHxsqLUdaucwIgJsJKl4gbHvff1pBHo/xRySByovEBeZESkrFZPszgx8UCIQCmzxKRluDAf/QvdPKNTnnm1qafy0g+Pdh5vnaCathhhA==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKvEAHucOqBPMS/2hyS5vvBEI7i/KJNQAncAysoAzjD9zLT05EUYTcSM/ry6JqeVBwI2BwLge/xPRoXO7TNfyB0CAwEAAQ==
password:krh9x0zn/ESXIgZPrMxBpyMef1bZ1t/vbwE9EcqdY0qTSi4iK3vnJfbyiWu7mT2A/uhx1MDnKzE3C1YT0Ry2lg==
2、配置application.properties
#mysql驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
#用户名
spring.datasource.username=root
# 启用ConfigFilter
spring.datasource.druid.filter.config.enabled=true
# 配置 connection-properties,启用加密,配置公钥。
public-key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKvEAHucOqBPMS/2hyS5vvBEI7i/KJNQAncAysoAzjD9zLT05EUYTcSM/ry6JqeVBwI2BwLge/xPRoXO7TNfyB0CAwEAAQ==
#加密并注入publicKey
spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${public-key}
#密码
spring.datasource.password=krh9x0zn/ESXIgZPrMxBpyMef1bZ1t/vbwE9EcqdY0qTSi4iK3vnJfbyiWu7mT2A/uhx1MDnKzE3C1YT0Ry2lg==
#连接池druid阿里巴巴
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
3、JAR包引入
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>