Druid實現數據庫鏈接用戶密碼加密

jar版本:druid-1.0.13.jarjava

1. 加密,用如下命令將用戶名和密碼加密mysql

cmd命令行執行 java -cp D:/druid-1.0.13.jar com.alibaba.druid.filter.config.ConfigTools 用戶名/密碼spring

獲得密文:sql

f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg==數據庫

2.用戶名解密:ide

ackage com.heli.core.user.common;
import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 用來解密配置中的密文(重點配置,在這裏擴展用戶名的解密)
* setUsername(name) 方法對應xml中的一個property屬性,password默認加密不須要重寫,
* 還能夠加密url 重寫setUrl(url) 
*/
@SuppressWarnings("all")
public class DecryptDruidSource extends DruidDataSource{
@Override
public void setUsername(String username) {
try {
username = ConfigTools.decrypt(username);
} catch (Exception e) {
e.printStackTrace();
}
super.setUsername(username);
}
}

3.spring-database.xml中數據庫鏈接的配置ui

<bean id="dataSource" class="com.heli.core.user.common.DecryptDruidSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
        <!-- config.decrypt=true -->
   <property name="filters" value="config" />
       <property name="connectionProperties" value="config.decrypt=true" />
      
<!-- 初始化鏈接大小 -->
<property name="initialSize" value="${initialSize}" />
<!-- 鏈接池最大使用鏈接數量 -->
<property name="maxActive" value="${maxActive}" />
<!-- 鏈接池最大空閒 這個參數已經被棄用 <property name="maxIdle" value="${maxIdle}"></property> -->
<!-- 鏈接池最小空閒 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 獲取鏈接最大等待時間 -->
<property name="maxWait" value="${maxWait}"></property>
         <property name="validationQuery" value="${validationQuery}" />
        <property name="testWhileIdle" value="${testWhileIdle}" />
        <property name="testOnBorrow" value="${testOnBorrow}" />
        <property name="testOnReturn" value="${testOnReturn}" />
 
        <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
        <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
 
        <!-- 關閉長時間不使用的鏈接 -->
        <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandoned" value="${removeAbandoned}" />
        <!-- 1200秒,也就是20分鐘 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
        <!-- 關閉abanded鏈接時輸出錯誤日誌 -->
        <property name="logAbandoned" value="${logAbandoned}" />
</bean>

4.數據庫配置文件:
加密

3.user.properties中
#mysql
username=f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg==
password=f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg==
url=jdbc:mysql://192.168.1.194/user?characterEncoding=utf-8
driver=com.mysql.jdbc.Driver
initialSize=5
minIdle=5
maxActive=20
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=30000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=true
testOnReturn=true
filters=stat,log4j
removeAbandoned=true
removeAbandonedTimeout=1200
logAbandoned=true
相關文章
相關標籤/搜索