Tomcat數據源連接池加密
我們在使用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”為你的自定義類,下面是相關代碼:
package net.uni.ap.jdbc;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import com.fesco.fws.utils.TeaUtil;
/**
*
* @author sunwill
*
*/
public class EncryptedDataSourceFactory extends BasicDataSourceFactory {
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable environment) throws Exception {
if (obj instanceof Reference) {
setUsername((Reference) obj);
setPassword((Reference) obj);
}
return super.getObjectInstance(obj, name, nameCtx, environment);
}
private void setUsername(Reference ref) throws Exception {
findDecryptAndReplace("username", ref);
}
private void setPassword(Reference ref) throws Exception {
findDecryptAndReplace("password", ref);
}
private void findDecryptAndReplace(String refType, Reference ref)
throws Exception {
int idx = find(refType, ref);
String decrypted = decrypt(idx, ref);
replace(idx, refType, decrypted, ref);
}
private void replace(int idx, String refType, String newValue, Reference ref)
throws Exception {
ref.remove(idx);
ref.add(idx, new StringRefAddr(refType, newValue));
}
private String decrypt(int idx, Reference ref) throws Exception {
return TeaUtil.decryptByTea(ref.get(idx).getContent().toString());
}
private int find(String addrType, Reference ref) throws Exception {
Enumeration enu = ref.getAll();
for (int i = 0; enu.hasMoreElements(); i++) {
RefAddr addr = (RefAddr) enu.nextElement();
if (addr.getType().compareTo(addrType) == 0) {
return i;
}
}
throw new Exception("The \"" + addrType
+ "\" name/value pair was not found"
+ " in the Reference object. The reference Object is" + " "
+ ref.toString());
}}其中紅色的地方是你的數據庫密碼解密方法,當然對應的也要有加密算法,加密后的串放到連接池的地方:
<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>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!