使用Druid生成加密密码,实现mysql数据库连接用户密码加密解密

来源:互联网 发布:软件产品说明书怎么写 编辑:程序博客网 时间:2024/05/01 01:23

参考 :Druid官网https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ConfigFilter的作用包括:

    从配置文件中读取配置
    从远程http文件中读取配置
    为数据库密码提供加密功能

1 配置ConfigFilter
1.1 配置文件从本地文件系统中读取

  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      init-method="init" destroy-method="close">
      <property name="filters" value="config" />
      <property name="connectionProperties" value="config.file=file:///home/admin/druid-pool.properties" />
  </bean>

1.2 配置文件从远程http服务器中读取

  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      init-method="init" destroy-method="close">
      <property name="filters" value="config" />
      <property name="connectionProperties" value="config.file=http://127.0.0.1/druid-pool.properties" />
  </bean>

这种配置方式,使得一个应用集群中,多个实例可以从同一个地方读取配置,集中配置,集中修改,部署更简单。
1.3 通过jvm启动参数来使用ConfigFilter

DruidDataSource支持jvm启动参数配置filters,所以你可以:

java -Ddruid.filters=config ....

2 数据库密码加密

数据库密码直接写在配置中,对运维安全来说,是一个很大的挑战。Druid为此提供一种数据库密码加密的手段ConfigFilter。
2.1 执行命令加密数据库密码

在命令行中执行如下命令:

java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password

输出

privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA6+4avFnQKP+O7bu5YnxWoOZjv3no4aFV558HTPDoXs6EGD0HP7RzzhGPOKmpLQ1BbA5viSht+aDdaxXp6SvtMQIDAQABAkAeQt4fBo4SlCTrDUcMANLDtIlax/I87oqsONOg5M2JS0jNSbZuAXDv7/YEGEtMKuIESBZh7pvVG8FV531/fyOZAiEA+POkE+QwVbUfGyeugR6IGvnt4yeOwkC3bUoATScsN98CIQDynBXC8YngDNwZ62QPX+ONpqCel6g8NO9VKC+ETaS87wIhAKRouxZL38PqfqV/WlZ5ZGd0YS9gA360IK8zbOmHEkO/AiEAsES3iuvzQNYXFL3x9Tm2GzT1fkSx9wx+12BbJcVD7AECIQCD3Tv9S+AgRhQoNcuaSDNluVrL/B/wOmJRLqaOVJLQGg==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOvuGrxZ0Cj/ju27uWJ8VqDmY7956OGhVeefB0zw6F7OhBg9Bz+0c84RjzipqS0NQWwOb4kobfmg3WsV6ekr7TECAwEAAQ==
password:PNak4Yui0+2Ft6JSoKBsgNPl+A033rdLhFw+L0np1o+HDRrCo9VkCuiiXviEMYwUgpHZUFxb2FpE0YmSguuRww==

输入你的数据库密码,输出的是加密后的结果。
2.2 配置数据源,提示Druid数据源需要对数据库密码进行解密。

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      init-method="init" destroy-method="close">
      <property name="url" value="jdbc:derby:memory:spring-test;create=true" />
      <property name="username" value="sa" />
      <property name="password" value="${password}" />
      <property name="filters" value="config" />
      <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publickey}" />
  </bean>

2.3 配置参数,让ConfigFilter解密密码

有三种方式配置:
1) 可以在配置文件my.properties中指定config.decrypt=true
2) 也可以在DruidDataSource的ConnectionProperties中指定config.decrypt=true

3) 也可以在jvm启动参数中指定-Ddruid.config.decrypt=true

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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


原创粉丝点击