生产环境c3p0数据源用户名密码加密或解密

直接上代码,相信大部分人能看懂

Spring applicationContext.xml

 
          
          
                
                    ${jdbc.password}
                
                  
                
                    ${jdbc.password.encrypted}
                
        	
   		
        
        
        
        
       	
        
        
          
        
         
    

注意,这里有个先后顺序的问题, 必须要在前面


import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.biznest.mediacircle.util.encrypt.CryptoUtil;

public class C3P0DatasourcePropertiesFactory {
	private static final String PRODUCTION_MODE = "true";
	private static final String PROP_PASSWORD = "password";
	private final static Logger logger = LoggerFactory.getLogger(C3P0DatasourcePropertiesFactory.class);

	public static Properties getProperties(String pwd, String production) throws Exception {
		Properties p = new Properties();

		if (PRODUCTION_MODE.equals(production)) {
			try {
				p.setProperty(PROP_PASSWORD, CryptoUtil.decode(pwd));//解密操作
			 catch (Exception e) {
				logger.error("C3P0DatasourcePropertiesFactory getProperties is error!", e);
				throw e;
			}
		}else{
			p.setProperty(PROP_PASSWORD, pwd);
		}
		return p;
	}
}


你可能感兴趣的:(JAVA)