1、使用jasypt
引用依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
加密
public class Test3 {
public static void main(String[] args) {
System.out.println(encrypt("123456","abc"));
}
public static String encrypt(String password, String factor) {
//加密工具
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//加密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
//秘钥
config.setPassword(factor);
//应用配置
encryptor.setConfig(config);
//加密
return encryptor.encrypt(password);
}
}
输出结果为:tjdji5QD0eTWEBaAxfAr8A==
然后修改配置文件为
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=ENC(tjdji5QD0eTWEBaAxfAr8A==)
jasypt.encryptor.password=abc
此加密方式还可以加密其他属性,只需要将属性用ENC()包起来即可,如果是yml配置文件,要加分号,即ENC();
2、使用druid
引用依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.14</version> </dependency>
加密
public class Test4 {
public static void main(String[] args) throws Exception {
String password = "123456";
System.out.println("明文密码: " + password);
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);
String decryptPassword = ConfigTools.decrypt(publicKey, password);
System.out.println("解密后:" + decryptPassword);
}
}
输出结果为:
明文密码: 123456
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA0jDOtqzPeLLFlzLPLZ9iOYEkUscmwrpHgskd1lnJPatcR8W5EksZz58fUgIBr6TObieHQhvmJOkcO/M3W8sOoQIDAQABAkAFV9xJZIc5qEHBoDcID98tWw0TVaC0noum1Xn+svWcrayxALvi9Ikc41K+TPB5PTnBqVoc/n8kBXNYWviAfjspAiEA9TqHxVHg4Cm2evr4XVVEPlcR3NleZ2ZBNpjI/S5seDMCIQDbbEsHxn8c2Sooi/IqoC+Lpr3OUA+Hk9jb1TXGF2/Z2wIhANHkB+Mmgn+6lBpMhplz6pYk9f/9+GQtzDFLsfOpcWOrAiB/phpoJZPhRnz+JZv2LEY8qf4VvgGqz9oC2aB040GQrQIhANxW/ponAEQp9yF3Aqk1H5PX+0p1gPiOj/vWgTLmKwA6
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANIwzrasz3iyxZcyzy2fYjmBJFLHJsK6R4LJHdZZyT2rXEfFuRJLGc+fH1ICAa+kzm4nh0Ib5iTpHDvzN1vLDqECAwEAAQ==
password:C0Y8TP50h4wzxg4TbI2SEjNsAL+G6oQn4O/cDPP5S99KWiZGEA5QhkVIY84zjmKNI6GBt3DsJ8UKi5VIaODucw==
解密后:123456
然后修改配置文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=C0Y8TP50h4wzxg4TbI2SEjNsAL+G6oQn4O/cDPP5S99KWiZGEA5QhkVIY84zjmKNI6GBt3DsJ8UKi5VIaODucw==
spring.datasource.druid.filter.config.enabled=true
spring.datasource.druid.connect-properties.config.decrypt=true
spring.datasource.druid.connect-properties.config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANIwzrasz3iyxZcyzy2fYjmBJFLHJsK6R4LJHdZZyT2rXEfFuRJLGc+fH1ICAa+kzm4nh0Ib5iTpHDvzN1vLDqECAwEAAQ==