在spirng容器装配dataSource的属性时,获取jdbc配置文件中对应的值,先解密

       jdbc.properties配置文件中的信息(用户名、用户密码、url)是经过加密的,然后放在系统的WEB-INF文件夹下供系统使用。由于这些信息是加密的,系统将数据库连接交给了spring管理,所以在spring初始化datasource-bean装配这些属性对应的值时,必须先解密。

 

<!--修改前spirng的配置文件:-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
	<list>
	<value>classpath:jdbc.properties</value>
	</list>
	</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${driverClass}"></property>
	<property name="jdbcUrl" value="${jdbcUrl}"></property>
	<property name="user" value="${username}"></property>
	<property name="password" value="${password}"></property>
	<property name="initialPoolSize" value="${initialPoolSize}"></property>
	<property name="maxPoolSize" value="${maxPoolSize}"></property>
	<property name="minPoolSize" value="${minPoolSize}"></property>
	<property name="acquireIncrement" value="${acquireIncrement}"></property>
	<property name="checkoutTimeout" value="${checkoutTimeout}"></property>
	<property name="acquireRetryAttempts" value="${acquireRetryAttempts}"></property>
</bean>

 

//org.springframework.beans.factory.config.PropertyPlaceholderConfi//gurer这个类是用来从指定的配置文件中取值,替换bean中的${}。我们现在要
//做的是,在从指定的配置文件中取出值返回时,对该值进行解密,再返回。对该
//类进行扩展,代码如下:

import java.util.Properties;

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

public class MyPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {
	
	
	@Override
	protected String resolvePlaceholder(String placeholder, Properties props) {
		String val = props.getProperty(placeholder);
		if(placeholder.equals("jdbcUrl") || placeholder.equals("username") || placeholder.equals("password")) {
			String result = DESDecrypt.execDecrypt(val);
		    return result;
		} else {
			return val;
		}
		 
	}
}

 

<!-- 修改后的spirng配置文件 -->
<bean class="cn.com.jit.pki.util.MyPropertyPlaceholderConfigurer">
	<property name="locations">
	<list>
	<value>classpath:jdbc.properties</value>
	</list>
	</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${driverClass}"></property>
	<property name="jdbcUrl" value="${jdbcUrl}"></property>
	<property name="user" value="${username}"></property>
	<property name="password" value="${password}"></property>
	<property name="initialPoolSize" value="${initialPoolSize}"></property>
	<property name="maxPoolSize" value="${maxPoolSize}"></property>
	<property name="minPoolSize" value="${minPoolSize}"></property>
	<property name="acquireIncrement" value="${acquireIncrement}"></property>
	<property name="checkoutTimeout" value="${checkoutTimeout}"></property>
	<property name="acquireRetryAttempts" value="${acquireRetryAttempts}"></property>
</bean>

 

你可能感兴趣的:(dataSource)