廢話不多說,直接貼代碼,畢竟搜這個的就是想知道怎麼搞。
1. 依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
2. 代碼
package net.mshome.twisted.tmall;
import com.alibaba.druid.filter.config.ConfigTools;
import org.junit.Test;
/**
* @author [email protected]
* @date 2019/9/1
* @description druid密碼加密
*/
public class DruidTest {
@Test
public void generateEncryptPasswordTest() throws Exception {
String password = "your password here";
ConfigTools.main(new String[]{password});
}
}
3. 配置
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/tmall?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true
username: tmall
password: OwikKIgJr19o4kD/vGjwftCuBw38Ykj0uc7/L3vIzo2fel6ncFPjxP2NI6c50lfkAxk4mvU8grPRK1e+uuoVDw==
public-key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKmZEOoiXoua+jIUht/oYTDrZ4YKNbQhQkCM//gAy+zgaEVFdc6B83Honl1vGnv1FxHndX8oRQI1grK8rT/ClWkCAwEAAQ==
filter:
config:
enabled: true
connect-properties:
config.decrypt: true
config.decrypt.key: ${spring.datasource.druid.public-key}
4. 解釋
- yourPassword + privateKey = encryptedPassword
- encryptedPassword + publicKey = yourPassword