druid连接池-数据库密码的加密与解密

druid连接池的加密与解密

    • 1. 加密
    • 2. 解密

1. 加密

根据原始密码生成加密密钥
首先进入druid的 Jar包所在的目录
druid连接池-数据库密码的加密与解密_第1张图片打开命令行 输入一下:

java -cp druid-1.0.18.jar com.alibaba.druid.filter.config.ConfigTools 123456
  • druid-1.0.18.jar 为jar 的包名
  • 123456 为原始密码

生成的大概如下示例:

  • 需要用到 publicKey 和 password
  • privateKey:私钥,用于生成密文密码用,不用管
privateKey:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAqbNMEelj7ai8wzhcrgpVG2364xhKB12MTjCHHS8veiuBLAIzrHQmNCi/VIPbp5CtlUxcm5o/jZU0q6nmyfbRRwIDAQABAkAaK7+Qh5jUnxA1XyzT3l4Ps99cRHOuPRwVNuCvfIZq5ahQSNu7wQqM0aoGR9Q/7CL00lH7OccbA7mJVhDj7JlxAiEA2zeB7/lbOXQZAb1/5sntP1+TpitbDhhecJmCI6QfEx8CIQDGLM2RazGwWBseBMuClnRFYW6uWD23ObhvstO9hmjk2QIgTEjWqMGnYyjpgteE6LUgIEuMP+kaeJ82Tgy9YtW8EpUCIAe9uEn15fr9dhG2Hjz7o48SP0CQMdj2aD30vlCo73ABAiAe/6yBUV+sUVISQ+TKMTZfm9rwRMRMHpHfrQ5fhbitzQ==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKmzTBHpY+2ovMM4XK4KVRtt+uMYSgddjE4whx0vL3orgSwCM6x0JjQov1SD26eQrZVMXJuaP42VNKup5sn20UcCAwEAAQ==
password:Z8w7Q6hQ87k7hfcREdK5oIAMPBMjBX4YzazHfz0SR42sOFyOW5DTRwV18blXkFJ8EftvKkQMuRw5YxFF8al+Qw==

applicationContext.xml 的数据源配置如下:

<!-- 加载资源文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:dbconfig.properties</value>
				<value>classpath:application-config.properties</value>
			</list>
		</property>
</bean>
<!-- 定义数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<property name="driverClassName" value="${driverClassName}" />
		<property name="filters" value="${filters}" />
		<property name="maxActive" value="${maxActive}" />
		<property name="initialSize" value="${initialSize}" />
		<property name="maxWait" value="${maxWait}" />
		<property name="minIdle" value="${minIdle}" />
		<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />

		<property name="validationQuery" value="${validationQuery}" />
		<property name="testWhileIdle" value="${testWhileIdle}" />
		<property name="testOnBorrow" value="${testOnBorrow}" />
		<property name="testOnReturn" value="${testOnReturn}" />
		<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
		<property name="removeAbandoned" value="${removeAbandoned}" />
		<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
		<property name="logAbandoned" value="${logAbandoned}" />
        <property name="connectionProperties"  value="druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${publickey}"/>

圈重点:

<property name="password" value="${password}" />
<property name="filters" value="${filters}" />
<property name="connectionProperties"  value="druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${publickey}"/>

password 和 publickey 为第一步加密后的密码和公钥
filters 配置为 filters , 如果需要和其他并用的用都好隔开 如filters,stat
配置文件如下:

url:jdbc:oracle:thin:@//xxx.xxx.xxx.xxx:1521/servicename
driverClassName:oracle.jdbc.OracleDriver
username:xxxxxx
publickey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJpWmscUpdp65cPU3+HboZ7Rgxx3T7CkB4BZZZ8Qf924qsfwj9l4gfFzTcIxMBN2y5aqTOUoQluZVGlQYN71um0CAwEAAQq==
password:VynK4fLjcNLvTT50btyFnZcmoy7itQswUw6EZh/A0DAp29B5eroVrPap0QMu5pzkwzO5mAPYlzZs2rf+00etqQq==
filters:config,stat
maxActive:50
initialSize:20
maxWait:60000
minIdle:20
maxIdle:15

timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000

validationQuery:select 1 from dual
testWhileIdle:true
testOnBorrow:true
testOnReturn:false
poolPreparedStatements:true
maxOpenPreparedStatements:20

removeAbandoned:true
removeAbandonedTimeout:1800
logAbandoned:true

2. 解密

  • pwd 加密后的password
  • pub 加密产生的公钥
  public static void main(String[] args) {
     
         String pwd = "Z8w7Q6hQ87k7hfcREdK5oIAMPBMjBX4YzazHfz0SR42sOFyOW5DTRwV18blXkFJ8EftvKkQMuRw5YxFF8al+Qw==";
         String pub = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKmzTBHpY+2ovMM4XK4KVRtt+uMYSgddjE4whx0vL3orgSwCM6x0JjQov1SD26eQrZVMXJuaP42VNKup5sn20UcCAwEAAQ==";
      try {
     
          String depwd = ConfigTools.decrypt(pub, pwd);
          System.out.println(depwd);
      } catch (Exception e) {
     
          e.printStackTrace();
      }
  }

运行改main方法即可输出原始密码

你可能感兴趣的:(♛,数据库,♛,druid,druid加密,druid解密)