您的位置:首页 > 产品设计 > UI/UE

Druid实现数据库连接用户密码加密

2017-03-29 11:11 477 查看
摘要: Druid官网https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter

jar包版本:druid-1.0.13.jar

1. 加密,用以下命令将用户名和密码加密

cmd命令行执行 java -cp D:/druid-1.0.13.jar com.alibaba.druid.filter.config.ConfigTools 用户名/密码

得到密文:

f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg==

2.用户名解密:

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中数据库连接的配置

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: