依赖
<!-- druid数据源驱动 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
加密
新建个测试方法,加密下得到密文跟公钥。
import com.alibaba.druid.filter.config.ConfigTools;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DruidTest {
@Test
public void encrypt() 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:" + decryptPassword);
}
}
得到私钥(privatekey )、公钥(publickey )、加密后的密码(password)如下:
明文密码: 123456
privatekey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAjmtKvCDGiybqxDWVvEkCeSifnSkvOcsEHZmBfac6noUMl8oOl1DwY4N66Yfalgs/0RGh2BWT5maYYgrLxy7+RwIDAQABAkEAh8NLqO7tlXTx0zzpkmVe0DSjg2PhOwyGzdipkii6PqVRBs8bzL74vGrp9j2SFq0NegdJVR5hgRO4JfEp8k/5cQIhAOMWIeMqvodDberoLfJ+I9+cU1s+rItWvPaDmiG31oktAiEAoI1u47C2rW2p1RJLA6MbOj+oC1QR8kvl4UMEWJVVJcMCIEHyM3Uc/KebVVhahL1fFFvjsfFVihYM4mNUZHZWRN8hAiEAleX21lay+BEfM3Q03Vx3m1a7ingfmV0Lb9mmQR6LtQkCIDx67fr+qHABDTVugYOEFqf7sdcIurE8wYelOGWLiFZM
publickey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI5rSrwgxosm6sQ1lbxJAnkon50pLznLBB2ZgX2nOp6FDJfKDpdQ8GODeumH2pYLP9ERodgVk+ZmmGIKy8cu/kcCAwEAAQ==
password:Aw0LEdG1J0M4jtOdC7or7xSlsCclnmkMdJgQ+c9slXmny67Z9Y//xy1wlihsnkSLFx9XhFe7/pcPn6Lg09IdGw==
decryptPassword:123456
配置
spring:
datasource:
# 使用阿里的Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
# 填写你数据库的url、登录名、密码和数据库名
druid:
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test
username: sa
password: Aw0LEdG1J0M4jtOdC7or7xSlsCclnmkMdJgQ+c9slXmny67Z9Y//xy1wlihsnkSLFx9XhFe7/pcPn6Lg09IdGw==
publickey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI5rSrwgxosm6sQ1lbxJAnkon50pLznLBB2ZgX2nOp6FDJfKDpdQ8GODeumH2pYLP9ERodgVk+ZmmGIKy8cu/kcCAwEAAQ==
# 这里有个坑
connect-properties:
config.decrypt: true
config.decrypt.key: ${spring.datasource.druid.publickey}
# 初始连接数
initialSize: 10
# 最大连接池数量
maxActive: 50
# 最小连接池数量
minIdle: 10
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
url-pattern: /monitor/druid/*
filter:
config:
enabled: true
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true