jar包版本:druid-1.0.15.jar
1. 加密,用以下命令将用户名和密码加密
cmd命令行执行
java -cp druid-1.0.15.jar com.alibaba.druid.filter.config.ConfigTools 加密串
得到密文
2.用户名解密:
1package com.heli.core.user.common; 2import com.alibaba.druid.filter.config.ConfigTools; 3import com.alibaba.druid.pool.DruidDataSource; 4/** 5* 用来解密配置中的密文(重点配置,在这里扩展用户名的解密) 6* setUsername(name) 方法对应xml中的一个property属性,password默认加密不需要重写, 7* 还可以加密url 重写setUrl(url) 8*/ 9@SuppressWarnings("all") 10public class DecryptDruidSource extends DruidDataSource{ 11@Override 12public void setUsername(String username) { 13 try { 14 username = ConfigTools.decrypt(username); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 super.setUsername(username); 19 } 20}
3.spring-database.xml中数据库连接的配置
1<bean id="dataSource" class="com.heli.core.user.common.DecryptDruidSource"> 2<property name="driverClassName" value="${driver}" /> 3<property name="url" value="${url}" /> 4<property name="username" value="${username}" /> 5<property name="password" value="${password}" /> 6 <!-- config.decrypt=true --> 7 <property name="filters" value="config" /> 8 <property name="connectionProperties" value="config.decrypt=true" /> 9 10<!-- 初始化连接大小 --> 11<property name="initialSize" value="${initialSize}" /> 12<!-- 连接池最大使用连接数量 --> 13<property name="maxActive" value="${maxActive}" /> 14<!-- 连接池最大空闲 这个参数已经被弃用 <property name="maxIdle" value="${maxIdle}"></property> --> 15<!-- 连接池最小空闲 --> 16<property name="minIdle" value="${minIdle}"></property> 17<!-- 获取连接最大等待时间 --> 18<property name="maxWait" value="${maxWait}"></property> 19 <property name="validationQuery" value="${validationQuery}" /> 20 <property name="testWhileIdle" value="${testWhileIdle}" /> 21 <property name="testOnBorrow" value="${testOnBorrow}" /> 22 <property name="testOnReturn" value="${testOnReturn}" /> 23 24 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 25 <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> 26 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 27 <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> 28 29 <!-- 关闭长时间不使用的连接 --> 30 <!-- 打开removeAbandoned功能 --> 31 <property name="removeAbandoned" value="${removeAbandoned}" /> 32 <!-- 1200秒,也就是20分钟 --> 33 <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> 34 <!-- 关闭abanded连接时输出错误日志 --> 35 <property name="logAbandoned" value="${logAbandoned}" /> 36</bean>
4.数据库配置文件:
1#mysql 2username=f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg== 3password=f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg== 4url=jdbc:mysql://192.168.1.194/user?characterEncoding=utf-8 5driver=com.mysql.jdbc.Driver 6initialSize=5 7minIdle=5 8maxActive=20 9maxWait=60000 10timeBetweenEvictionRunsMillis=60000 11minEvictableIdleTimeMillis=30000 12validationQuery=SELECT 1 13testWhileIdle=true 14testOnBorrow=true 15testOnReturn=true 16filters=stat,log4j 17removeAbandoned=true 18removeAbandonedTimeout=1200 19logAbandoned=true