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

 

转载于:https://www.cnblogs.com/benefitworld/p/5832668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值