(轉)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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM