加密jdbc配置文件中的用户名密码

我们使用的项目经常是这个样子的:
  1. <bean id="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中来:
  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
  2.     p:location="classpath:jdbc.properties"  
  3.     p:fileEncoding="utf-8"  
  4.     />  
  5.       
  6. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  7.     destroy-method="close"   
  8.     p:driverClassName="${driverClassName}"  
  9.     p:url="${url}"   
  10.     p:username="${userName}"  
  11.     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就可以了:

  1. package com.cardDemo.commonUtil;  
  2.   
  3. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
  4.   
  5. public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer{  
  6.     @Override  
  7.     protected String convertProperty(String propertyName, String propertyValue) {  
  8.         System.out.println("=================="+propertyName+":"+propertyValue);  
  9.         if("userName".equals(propertyName)){  
  10.             return "czw";  
  11.         }  
  12.         if("password".equals(propertyName)){  
  13.             return "czw";  
  14.         }  
  15.         return propertyValue;  
  16.     }  
  17. }  
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:

  1. <bean class="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里面的文件是如下内容的:

  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:ziwen@163.com  QQ:409020100

如果有技术面试官再问到你,怎么隐藏在配置文件中的用户名和密码的时候,希望能够说出这个思路,如果能够提到PropertyPlaceholderConfigurer的子类中的convertProperty方法的话,就更加出彩了。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <description>Spring公共配置文件</description> <!-- mes 的數據庫 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mestest"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="mesDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码用户名 --> <bean id="mesDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- ptc windchill的數據庫 --> <bean id="dataSourcePdm" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mesdev"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="ptcDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码用户名 --> <bean id="ptcDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- mes數據源代理 --> <bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy" p:targetDataSource-ref="dataSource"/> <!-- 对web包的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--> <context:component-scan base-package="com.ccc"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /> <!-- 配置事务管理器 針對MES數據庫--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourceProxy"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.ccc..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config> <!-- 配置事务管理器,這個事務性是爭對pdm數據庫的 --> <bean id="transactionManagerPdm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourcePdm"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvicePdm" transaction-manager="transactionManagerPdm"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethodPdm" expression="execution(* com.ccc.pdm..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethodPdm" advice-ref="txAdvicePdm"/> </aop:config> <!-- ibatis插件 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- 配置要拦截的url,防止2次提交或做其他數據統計用 <bean id="doubleSubmitInterceptor" class="com.ccc.filter.DoubleSubmitInterceptor"> <property name="mappingURL" value=".html" /> <property name="viewURL" value=".html" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> </property> </bean> --> <!-- JDBC template注入及事務配置 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="dataSourceProxy"/></property> </bean> </beans>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值