Java

Druid连接池 加密 与解密

2017-10-19  本文已影响0人  郝仁品Sam哥

Druid连接池 加密 与解密

(spring_mybatis_druid)

加密

1.控制台加密密码

java -cp /Users/shutakaratakara/.m2/repository/com/alibaba/druid/1.1.2/druid-1.1.2.jar com.alibaba.druid.filter.config.ConfigTools 密码  

2.得到加密码后数据

privateKey:
publicKey:
password:

3.测试解码

 public void testCode(){
        String publicKey="加密后数据";
        String pwd = "加密后数据";
        try {
            String decryptword=ConfigTools.decrypt(publicKey,pwd);
            System.out.println(decryptword);
        }
         catch (Exception e) {
            e.printStackTrace();
        }
    }

4.Spring配置文件

<util:properties id="cfg" location="classpath:dbconfig.properties"/>
<!-- 阿里 druid 数据库连接池 -->
<bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method="close" >
...
    <!-- 数据库基本信息配置 -->
    <!-- config.decrypt=true -->
    <property name="filters" value="config" />
    <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=#{cfg.publicKey}" />
    ...
</bean>

5.dbconfig.properties(主要password,publicKey)

url=jdbc:mysql://localhost:3306/数据库
driverClassName=com.mysql.jdbc.Driver
username=root
password=加密后数据
publicKey=加密后数据
filters=stat
maxActive=20
initialSize=1
maxWait=60000
minIdle=10
maxIdle=15
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
maxOpenPreparedStatements=20
removeAbandoned=true
removeAbandonedTimeout=1800
logAbandoned=true

druid参考配置

<!-- 阿里 druid 数据库连接池 -->
    <bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method="close" >
        <!-- 数据库基本信息配置 -->
        <property name = "url" value = "#{cfg.url}" />
        <property name = "username" value = "#{cfg.username}" />
        <property name = "password" value = "#{cfg.password}" />
        <!-- config.decrypt=true -->
        <property name="filters" value="config" />
        <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=#{cfg.publicKey}" />
        <property name = "driverClassName" value = "#{cfg.driverClassName}" />
        <!--<property name = "filters" value = "#{cfg.filters}" />-->
        <!-- 最大并发连接数 -->
        <property name = "maxActive" value = "#{cfg.maxActive}" />
        <!-- 初始化连接数量 -->
        <property name = "initialSize" value = "#{cfg.initialSize}" />
        <!-- 配置获取连接等待超时的时间 -->
        <property name = "maxWait" value = "#{cfg.maxWait}" />
        <!-- 最小空闲连接数 -->
        <property name = "minIdle" value = "#{cfg.minIdle}" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name = "timeBetweenEvictionRunsMillis" value ="#{cfg.timeBetweenEvictionRunsMillis}" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name = "minEvictableIdleTimeMillis" value ="#{cfg.minEvictableIdleTimeMillis}" />
        <property name = "validationQuery" value = "#{cfg.validationQuery}" />
        <property name = "testWhileIdle" value = "#{cfg.testWhileIdle}" />
        <property name = "testOnBorrow" value = "#{cfg.testOnBorrow}" />
        <property name = "testOnReturn" value = "#{cfg.testOnReturn}" />
        <property name = "maxOpenPreparedStatements" value ="#{cfg.maxOpenPreparedStatements}" />
        <!-- 打开 removeAbandoned 功能 -->
        <property name = "removeAbandoned" value = "#{cfg.removeAbandoned}" />
        <!-- 1800 秒,也就是 30 分钟 -->
        <property name = "removeAbandonedTimeout" value ="#{cfg.removeAbandonedTimeout}" />
        <!-- 关闭 abanded 连接时输出错误日志 -->
        <property name = "logAbandoned" value = "#{cfg.logAbandoned}" />
    </bean>
上一篇 下一篇

猜你喜欢

热点阅读