Druid連接池 加密 與解密
(spring_mybatis_druid)
加密
1.控制臺(tái)加密密碼
java -cp /Users/shutakaratakara/.m2/repository/com/alibaba/druid/1.1.2/druid-1.1.2.jar com.alibaba.druid.filter.config.ConfigTools 密碼
2.得到加密碼后數(shù)據(jù)
privateKey:
publicKey:
password:
3.測試解碼
public void testCode(){
String publicKey="加密后數(shù)據(jù)";
String pwd = "加密后數(shù)據(jù)";
try {
String decryptword=ConfigTools.decrypt(publicKey,pwd);
System.out.println(decryptword);
}
catch (Exception e) {
e.printStackTrace();
}
}
4.Spring配置文件
<util:properties id="cfg" location="classpath:dbconfig.properties"/>
<!-- 阿里 druid 數(shù)據(jù)庫連接池 -->
<bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method="close" >
...
<!-- 數(shù)據(jù)庫基本信息配置 -->
<!-- config.decrypt=true -->
<property name="filters" value="config" />
<property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=#{cfg.publicKey}" />
...
</bean>
5.dbconfig.properties(主要password,publicKey)
url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫
driverClassName=com.mysql.jdbc.Driver
username=root
password=加密后數(shù)據(jù)
publicKey=加密后數(shù)據(jù)
filters=stat
maxActive=20
initialSize=1
maxWait=60000
minIdle=10
maxIdle=15
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
maxOpenPreparedStatements=20
removeAbandoned=true
removeAbandonedTimeout=1800
logAbandoned=true
druid參考配置
<!-- 阿里 druid 數(shù)據(jù)庫連接池 -->
<bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method="close" >
<!-- 數(shù)據(jù)庫基本信息配置 -->
<property name = "url" value = "#{cfg.url}" />
<property name = "username" value = "#{cfg.username}" />
<property name = "password" value = "#{cfg.password}" />
<!-- config.decrypt=true -->
<property name="filters" value="config" />
<property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=#{cfg.publicKey}" />
<property name = "driverClassName" value = "#{cfg.driverClassName}" />
<!--<property name = "filters" value = "#{cfg.filters}" />-->
<!-- 最大并發(fā)連接數(shù) -->
<property name = "maxActive" value = "#{cfg.maxActive}" />
<!-- 初始化連接數(shù)量 -->
<property name = "initialSize" value = "#{cfg.initialSize}" />
<!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
<property name = "maxWait" value = "#{cfg.maxWait}" />
<!-- 最小空閑連接數(shù) -->
<property name = "minIdle" value = "#{cfg.minIdle}" />
<!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 -->
<property name = "timeBetweenEvictionRunsMillis" value ="#{cfg.timeBetweenEvictionRunsMillis}" />
<!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
<property name = "minEvictableIdleTimeMillis" value ="#{cfg.minEvictableIdleTimeMillis}" />
<property name = "validationQuery" value = "#{cfg.validationQuery}" />
<property name = "testWhileIdle" value = "#{cfg.testWhileIdle}" />
<property name = "testOnBorrow" value = "#{cfg.testOnBorrow}" />
<property name = "testOnReturn" value = "#{cfg.testOnReturn}" />
<property name = "maxOpenPreparedStatements" value ="#{cfg.maxOpenPreparedStatements}" />
<!-- 打開 removeAbandoned 功能 -->
<property name = "removeAbandoned" value = "#{cfg.removeAbandoned}" />
<!-- 1800 秒,也就是 30 分鐘 -->
<property name = "removeAbandonedTimeout" value ="#{cfg.removeAbandonedTimeout}" />
<!-- 關(guān)閉 abanded 連接時(shí)輸出錯(cuò)誤日志 -->
<property name = "logAbandoned" value = "#{cfg.logAbandoned}" />
</bean>