1、application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#數(shù)據(jù)庫賬號(hào)
spring.datasource.username=K4NWAGWajCMwIOpm/QUgZwh+kY+dXYaS2C8UbSQlYh7uCsxv8fPPm3j1ObQ==
#數(shù)據(jù)庫密碼
spring.datasource.password=PrV7ZKPAMsHNO+nOyUb6kg44wDdKEL8xrhrEPAv4fXFS2ijLNW044glIQ==
# DataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#加上config
spring.datasource.filters=stat,wall,log4j,config
#加上config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}
#公鑰
spring.datasource.publickey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIWtxv6jBv0l662q/T9UFnQNia92Uht929bH0BrJzSqzMoYMWffmzFdLW9Zp0xJpfER8qCGMc+3zyyPwHe0jYAcCAwEAAQ==
spring.datasource.druid.filter.config.enabled=true
2、數(shù)據(jù)源配置類
datasource.setUrl(this.dbUrl);
datasource.setUsername(ConfigTools.decrypt(publickey,username));
datasource.setPassword(ConfigTools.decrypt(publickey,password));
3、附屬,加密類
import java.security.NoSuchProviderException;
import com.alibaba.druid.filter.config.ConfigTools;
public class DruidEncrypt {
public static void main(String[] args) throws Throwable, NoSuchProviderException {
//密碼
String password = "mypwd";
String username = "myuseraccount";
System.out.println("加密前的密碼:"+password);
//生成秘鑰對(duì)
String [] keyPair = ConfigTools.genKeyPair(512);
//私鑰
String privateKey = keyPair[0];
//公鑰
String publicKey = keyPair[1];
//用私鑰加密后的密文
username = ConfigTools.encrypt(privateKey, username);
password = ConfigTools.encrypt(privateKey, password);
System.out.println("privateKey:"+privateKey);
System.out.println("publicKey:"+publicKey);
System.out.println("加密后的用戶:"+username);
System.out.println("加密后的密碼:"+password);
String decryptUsername=ConfigTools.decrypt(publicKey, username);
String decryptPassword=ConfigTools.decrypt(publicKey, password);
System.out.println("解密后的username:"+decryptUsername);
System.out.println("解密后的password:"+decryptPassword);
}
}