Spring的xml文件配置加密密码

1.JDBC密码使用密文

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/fracong
jdbc.username=fracong
jdbc.password=UnUIpoEvwLVK2gK5og==

2.application.xml文件

<bean id="propertyConfigurer" class="com.fracong.config.DecryptPropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>/WEB-INF/classes/db.properties</value>
		</list>
	</property>
</bean>
// 这里就不要使用这个进行加载:<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

说明:
1.如果你想将password从环境变量中获取,那么可以使用如下:

<property name="password" value="#{systemEnvironment['FRACONG_TEST_JDBC_URL']}" />

2.propertyConfigurer的class为org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,这个时候需要自定义新的类,继承PropertyPlaceholderConfigurer类即可,重写processProperties或者convertProperty方法。

3.重写方法

方式1:重写processProperties方法

package com.fracong.config;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.fracong.Utils;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
    
    
	private static Log logger = LogFactory.getLog("fracong");
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
    
    
		try {
    
    
			String passwordEncrypt = props.getProperty("jdbc.password");
			String password = Utils.decryptProperty(passwordEncrypt); // 自定义工具类,对密码进行解密
			props.setProperty("jdbc.password", password);
		} catch (Exception e) {
    
    
			logger.error(e.getMessage());
		}
		super.processProperties(beanFactoryToProcess, props);
	}
}

方式1:重写convertProperty方法

package com.fracong.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.fracong.Utils;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
    
    
	private static Log logger = LogFactory.getLog("fracong");
	
	private String[] encryptPropNames = {
    
     "jdbc.password" };
	@Override
	protected String convertProperty(String propertyName, String propertyValue) {
    
    
		for (String property : encryptPropNames) {
    
    
			if(property.equals(propertyName)) {
    
    
				propertyValue =  Utils.decryptProperty(propertyValue);  // 自定义工具类,对密码进行解密
			}
		}
		return super.convertProperty(propertyName, propertyValue);
	}
}

猜你喜欢

转载自blog.csdn.net/m0_37356874/article/details/107492514