druid数据源的加密解密工具

数据库得加密

先来一个网上大多数的教程吧,一个比较好的教程,如下、

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

但是上面那个教程有个问题,在才cmd下的加密时一个随时在变的,我只有在工程里写下如下文件,才能进行加密以及解密
 1 package test;
 2 
 3 import org.junit.Test;
 4 
 5 import com.alibaba.druid.filter.config.ConfigTools;
 6 
 7 public class DecryptDruid {
 8     /**
 9      * 对文字进行解密
10      * @throws Exception
11      */
12     @Test
13     public  void testDecrypt() throws Exception {
14         //解密
15         String word="MMUcTIwe+HMRBUYAVqdozWhxSB+rjY/HIBo08LsxlPJ/ocVXrvcKPwaMgWEKkApeDylU8RGPOAqsjsNy7Xg+fQ==";
16         String decryptword = ConfigTools.decrypt(word);
17         System.out.println(decryptword);
18     }
19     /**
20      * 文字进行加密
21      * @throws Exception 
22      */
23     @Test
24     public void testEncrypt() throws Exception
25     {
26         //加密
27         String password ="xxxxxxx";
28         String encryptword = ConfigTools.encrypt(password);
29         System.out.println(encryptword);
30         
31     }
32 }

用上面弄出来的密码才是好的,不然密码不对



原文地址:https://www.cnblogs.com/renboqie/p/5665523.html