1. 程式人生 > >加密jdbc配置檔案中的使用者名稱密碼

加密jdbc配置檔案中的使用者名稱密碼

我們使用的專案經常是這個樣子的: [html] view plaincopyprint?
  1. <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
  2. destroy-method="close"
  3. p:driverClassName="oracle.jdbc.driver.OracleDriver"
  4. p:url="jdbc:oracle:thin:@127.0.0.1:1523:orcl"
  5. p:username="czw"
  6. p:password="czw"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close" 
	p:driverClassName="oracle.jdbc.driver.OracleDriver"
	p:url="jdbc:oracle:thin:@127.0.0.1:1523:orcl" 
	p:username="czw"
	p:password="czw" />


這裡會有一個致命的問題,如果有一個具備中介軟體伺服器機器訪問許可權的人,看到了這個例如applicationContext.xml的檔案,並且開啟該檔案,智商再低下的人也會知道資料庫的使用者名稱和密碼是什麼。這對於對安全有一定要求的行業是必須杜絕的,這個也是在一般技術面試中會問到的一個問題。那就讓我們繼續往下,解答這個問題吧! 首先,我們需要將配置檔案抽取到property中來: [html] view plaincopyprint?
  1. <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
  2. p:location="classpath:jdbc.properties"
  3. p:fileEncoding="utf-8"
  4. />
  5. <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
  6. destroy-method="close"
  7. p:driverClassName="${driverClassName}"
  8. p:url="${url}"
  9. p:username="${userName}"
  10. p:password="${password}"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
  	p:location="classpath:jdbc.properties"
  	p:fileEncoding="utf-8"
  	/>
  	
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close" 
	p:driverClassName="${driverClassName}"
	p:url="${url}" 
	p:username="${userName}"
	p:password="${password}" />


將上面的第一個程式碼修改為第二個程式碼,第一個類是負責抓取jdbc.properties中的屬性並且填充到dataSource當中來,這樣,我們就可以將所有的注意力都集中在jdbc.properties上了。 下面的問題是,如何將jdbc.properties變成一個看不明白的字元呢?我們只需要擴充套件PropertyPlaceholderConfigurer父類PropertyResourceConfigurer的解密方法convertProperty就可以了: [html] view plaincopyprint?
  1. package com.cardDemo.commonUtil;  
  2. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
  3. public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer{  
  4.     @Override  
  5.     protected String convertProperty(String propertyName, String propertyValue) {  
  6.         System.out.println("=================="+propertyName+":"+propertyValue);  
  7.         if("userName".equals(propertyName)){  
  8.             return "czw";  
  9.         }  
  10.         if("password".equals(propertyName)){  
  11.             return "czw";  
  12.         }  
  13.         return propertyValue;  
  14.     }  
  15. }  
package com.cardDemo.commonUtil;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer{
	@Override
	protected String convertProperty(String propertyName, String propertyValue) {
		System.out.println("=================="+propertyName+":"+propertyValue);
		if("userName".equals(propertyName)){
			return "czw";
		}
		if("password".equals(propertyName)){
			return "czw";
		}
		return propertyValue;
	}
}


然後將上面完成的類替換配置檔案中的PropertyPlaceholderConfigurer: [html] view plaincopyprint?
  1. <beanclass="com.cardDemo.commonUtil.ConvertPwdPropertyConfigurer"
  2. p:location="classpath:jdbc.properties"
  3. p:fileEncoding="utf-8"
  4. />
<bean class="com.cardDemo.commonUtil.ConvertPwdPropertyConfigurer"
	p:location="classpath:jdbc.properties"
	p:fileEncoding="utf-8"
	/>


事實上,在我剛剛的Demo專案當中,裡面的jdbc.properties裡面的檔案是如下內容的: [java] view plaincopyprint?
  1. driverClassName=oracle.jdbc.driver.OracleDriver  
  2. url=jdbc:oracle:thin:@127.0.0.1:1523:orcl  
  3. userName=someOneElseUnknowUserName  
  4. password=somePwdElseUnknowPassowrd  
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1523:orcl
userName=someOneElseUnknowUserName
password=somePwdElseUnknowPassowrd


而實際上,真實的密碼卻是czw/czw,web程式執行的時候,顯示如下的內容: 2013-8-31 13:26:12 org.apache.catalina.core.StandardEngine start 資訊: Starting Servlet Engine: Apache Tomcat/6.0.18 2013-8-31 13:26:14 org.apache.catalina.core.ApplicationContext log 資訊: Initializing Spring root WebApplicationContext ==================url:jdbc:oracle:thin:@127.0.0.1:1523:orcl ==================password:somePwdElseUnknowPassowrd ==================driverClassName:oracle.jdbc.driver.OracleDriver ==================userName:someOneElseUnknowUserName 2013-8-31 13:26:17 org.apache.catalina.core.ApplicationContext log 資訊: Initializing Spring FrameworkServlet 'cardDemo' 2013-8-31 13:26:18 org.apache.coyote.http11.Http11Protocol start 資訊: Starting Coyote HTTP/1.1 on http-8080 2013-8-31 13:26:18 org.apache.jk.common.ChannelSocket init 資訊: JK: ajp13 listening on /0.0.0.0:8009 但是,在DATASOURCE裡面獲取到的內容,卻是替換之後的正確的使用者名稱和密碼。這只是一個非常簡單的例子,只是告訴我們一個解決資料庫使用者名稱和密碼加密的一個渠道,比如,我們可以在PropertyPlaceholderConfigurer子類中寫一些比較複雜的邏輯,比如根據jdbc.properties中配置的檔案中進行各種手段的加密。在其中通過其他手段替換jdbc.propertes中的使用者名稱和密碼等等。 作者 陳字文(熱衷於PM\ORACLE\JAVA等,歡迎同行交流)EMAIL:[email protected]  QQ:409020100 如果有技術面試官再問到你,怎麼隱藏在配置檔案中的使用者名稱和密碼的時候,希望能夠說出這個思路,如果能夠提到PropertyPlaceholderConfigurer的子類中的convertProperty方法的話,就更加出彩了。