(转)Tomcat数据源连接池加密

Posted 沐浴春风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)Tomcat数据源连接池加密相关的知识,希望对你有一定的参考价值。

文章来源 :http://my.oschina.net/cimu/blog/164757

我们在使用Tomcat数据库连接池的时候都是明文存储数据库用户名和密码的,例如:

<Resource name="ODS" type="javax.sql.DataSource"
 driverClassName="oracle.jdbc.driver.OracleDriver"
 url="jdbc:oracle:thin:@192.168.1.1:1521:dbid"
 username="oracle"
 password="oracle"
 maxIdle="4"
 maxActive="6"
 maxWait="5000" />

如果我们不想让数据库的密码暴露在web容器中怎么办呢?写一个类继承org.apache.commons.dbcp.BasicDataSourceFactory,然后指定factory=”*.EncryptedDataSourceFactory”为你的自定义类,下面是相关代码:

 1 package net.uni.ap.jdbc;
 2 import java.util.Enumeration;
 3 import java.util.Hashtable;
 4 import javax.naming.Context;
 5 import javax.naming.Name;
 6 import javax.naming.RefAddr;
 7 import javax.naming.Reference;
 8 import javax.naming.StringRefAddr;
 9 import org.apache.commons.dbcp.BasicDataSourceFactory;
10 import com.fesco.fws.utils.TeaUtil;
11 /**
12  * 
13  * @author sunwill
14  * 
15  */
16 public class EncryptedDataSourceFactory extends BasicDataSourceFactory {
17  public Object getObjectInstance(Object obj, Name name, Context nameCtx,
18  Hashtable environment) throws Exception {
19  if (obj instanceof Reference) {
20  setUsername((Reference) obj);
21  setPassword((Reference) obj);
22  }
23  return super.getObjectInstance(obj, name, nameCtx, environment);
24  }
25 private void setUsername(Reference ref) throws Exception {
26  findDecryptAndReplace("username", ref);
27  }
28 private void setPassword(Reference ref) throws Exception {
29  findDecryptAndReplace("password", ref);
30  }
31 private void findDecryptAndReplace(String refType, Reference ref)
32  throws Exception {
33  int idx = find(refType, ref);
34  String decrypted = decrypt(idx, ref);
35  replace(idx, refType, decrypted, ref);
36  }
37 private void replace(int idx, String refType, String newValue, Reference ref)
38  throws Exception {
39  ref.remove(idx);
40  ref.add(idx, new StringRefAddr(refType, newValue));
41  }
42 private String decrypt(int idx, Reference ref) throws Exception {
43  return TeaUtil.decryptByTea(ref.get(idx).getContent().toString());
44  }
45 private int find(String addrType, Reference ref) throws Exception {
46  Enumeration enu = ref.getAll();
47  for (int i = 0; enu.hasMoreElements(); i++) {
48  RefAddr addr = (RefAddr) enu.nextElement();
49  if (addr.getType().compareTo(addrType) == 0) {
50  return i;
51  }
52  }
53  throw new Exception("The \"" + addrType
54  + "\" name/value pair was not found"
55  + " in the Reference object. The reference Object is" + " "
56  + ref.toString());
57  }}

其中红色的地方是你的数据库密码解密方法,当然对应的也要有加密算法,加密后的串放到连接池的地方:

<Context path="">
 <Resource name="ODS" type="javax.sql.DataSource"
 driverClassName="oracle.jdbc.driver.OracleDriver"
 factory="net.uni.ap.jdbc.EncryptedDataSourceFactory"
 url="jdbc:oracle:thin:@192.168.1.1:1521:sid"
 username="oracle"
 password="C65BD76C4CED33C446B289F64CAFACC5"
 maxIdle="4"
 maxActive="6"
 maxWait="5000" />
</Context>

 

以上是关于(转)Tomcat数据源连接池加密的主要内容,如果未能解决你的问题,请参考以下文章

c3p0,dbcp与druid 三大连接池的区别[转]

Tomcat 连接池详解

Tomcat数据库连接池配置

Tomcat 连接池 MBean 并不总是出现在扫描中

java sql数据连接池的问题?

hibernate和tomcat的连接池问题