druid数据源的加密解密工具

数据库得加密java

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

 

 

jar包版本:druid-1.0.13.jarspring

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

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

获得密文:less

f0PSl0Lzxh6CxzuFIdEg+wVx045fSE2VtUP45G9HH2cjVQnmGGgcK5CLzNUJoR6tGwRO44h74OxrBWuDzWC8jg==ide

2.用户名解密:ui

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.数据库配置文件:url

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 }

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

相关文章
相关标签/搜索