加密Spring加载的Properties文件

实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密。
一,编写工具类,继承PropertyPlaceholderConfigurer
package com.csair.cbd.utils;

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.csair.cbd.member.utils.CryptUtils;
import com.csair.common.exceptions.ServiceException;

/**
 * 配置文件解密类
 * @author liuyouyi
 * @company csair
 * 2013-10-21
 * sybase
 * com.csair.cbd.utils
 * @version 1.0
 *
 */
public class MyConfigurer extends PropertyPlaceholderConfigurer {  
	
	public final static String strKey = "4321aaaaaaaa1314aaaaalyy";
	private static final Log logger = LogFactory.getLog(MyConfigurer.class);
  
    @Override  
    protected void processProperties(  
            ConfigurableListableBeanFactory beanFactory, Properties props)  
            throws BeansException {  
  
       
        
        String jdbcPassword = props.getProperty("jdbc.password");
        if(jdbcPassword != null ) {
        	try {
        		//logger.info("----------------------------加密前-----------------------------");
				props.setProperty("jdbc.password", decrypt(jdbcPassword));
				//logger.info("----------------------------加密后-----------------------------");
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
       String jdbc2Password = props.getProperty("jdbc2.password");
        if(jdbc2Password != null ) {
        	try {
				props.setProperty("jdbc2.password", decrypt(jdbc2Password));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
        
        String iqjdbcPassword = props.getProperty("iqjdbc.password");
        if(iqjdbcPassword != null ) {
        	try {
				props.setProperty("iqjdbc.password", decrypt(iqjdbcPassword));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
        
        String jdbcMysqlPassword = props.getProperty("jdbc.mysql.password");
        if(jdbcMysqlPassword != null ) {
        	try {
				props.setProperty("jdbc.mysql.password", decrypt(jdbcMysqlPassword));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
       
        String mysqlJdbcPassword = props.getProperty("mysql.jdbc.password");
        if(mysqlJdbcPassword != null ) {
        	try {
				props.setProperty("mysql.jdbc.password", decrypt(mysqlJdbcPassword));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
        
        super.processProperties(beanFactory, props);       
  
    }  
    
    /**
	 * 加密
	 * 
	 * @param password
	 */
	public static String encrypt(String password) throws Exception {

		// SHA1加密
		String uid = CryptUtils.getInstance(CryptUtils.Algorithm_DESede)
				.encrypt(password, strKey);
		return uid;
	}

	/**
	 * 解密
	 * 
	 * @param password
	 */
	public static String decrypt(String password) throws ServiceException {
		try {
			return CryptUtils.getInstance(CryptUtils.Algorithm_DESede).decrypt(
					password, strKey);
		} catch (Exception e) {
			throw new ServiceException("解密失败!");
		}
	}
	
	
	public static void main(String[] args) {
		try {
			String string1 = encrypt("test_password");
			String string2 = decrypt(string1);
			//String string3 = ReserverCookie.decode(string2);
			System.out.println(string1+"--"+string2);			
		} catch (Exception e) {
		}
	}
    
}  



二,改写配置文件
<bean id="JDBCpropertyConfigurer"  
        class="com.csair.cbd.utils.MyConfigurer">  
        <property name="ignoreResourceNotFound" value="true" />  
        <property name="locations">  
            <list>  
                <value>classpath:jdbc.properties</value> 
				<value>classpath:system.properties</value>
				<value>classpath:sms-template.properties</value>
				<value>classpath:deploy/test.properties</value>
				<value>classpath:com/csair/cbd/member/member-verify.properties</value>
				<value>classpath:com/csair/cbd/rule/rule-message.properties</value> 
				<value>classpath:com/csair/cbd/integral/services/impl/deferregist-message.properties</value>
            </list>  
        </property>  
    </bean>  


三,整合其它的PropertyPlaceholderConfigurer配置,以免被覆盖
四,去掉context:property-placeholder,否则配置也是会被覆盖的
<!--context:property-placeholder location="classpath:jdbc.properties,classpath:system.properties,classpath:sms-template.properties,classpath:deploy/test.properties,classpath:com/csair/cbd/member/member-verify.properties,classpath:com/csair/cbd/rule/rule-message.properties"/-->
   	

你可能感兴趣的:(properties)