SpringBoot整合Druid Mysql数据库密码加密
添加maven依赖
<!--数据库密码加密--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
密码加密工具类
public class DecryptDruid {
public static void main(String[] args) throws Exception {
testEncrypt("123456");
}
/**
* 对指定的密码使用公钥进行解密
*
* @param passwd 加密后的密码
* @param publicKey 公钥
* @return 解密后的明文密码
* @throws Exception 異常
*/
public static String testDecrypt(String passwd, String publicKey) throws Exception {
// 解密
String decryptPassword = ConfigTools.decrypt(publicKey, passwd);
System.out.println("decryptPassword:" + decryptPassword);
return decryptPassword;
}
/**
* 对指定的明文密码,生成公私钥进行加密
*
* @param password 明文密码
* @return 密文密码
* @throws Exception 異常
*/
public static String testEncrypt(String password) throws Exception {
String[] keyPair = ConfigTools.genKeyPair(512);
//私钥
String privateKey = keyPair[0];
//公钥
String publicKey = keyPair[1];
//加密
password = ConfigTools.encrypt(privateKey, password);
System.out.println("privateKey:" + privateKey);
System.out.println("publicKey:" + publicKey);
System.out.println("password:" + password);
// 解密操作
testDecrypt(password, publicKey);
return password;
}
配置yml配置文件
datasource:
# 驱动
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://1.111.11.11:3306/abc?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useAffectedRows=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: root
password: jPvjuTx57IzAl16DTWsMlGGGcl49Qs8DOsvvnSuR1wmENxrvDeAOJIGBU4E0Ks5oNhE0ssA==
publickey: MFwwDQYJKoZIhvcNAQEBBwzSl9QLoz5n7lpFQrk5CKCdqjZ1bBWgrCNiVFmxHJhpk1jhQtSsapebF501cCAwEAAQ==
druid:
connect-properties:
config.decrypt: true
config.decrypt.key: ${spring.datasource.publickey}
filter:
config:
enabled: true # 启动ConfigFilter
下一篇:
解决JSP中文显示问题的几个方法
