spring-jdbc文件数据库配置加密

  一般spring容器启动时,通过PropertyPlaceholderConfigurer类读取jdbc.properties文件里的数据库配置信息。

通过这个原理,我们把加密后的数据库配置信息放到jdbc.properties文件里,然后自定义一个继承PropertyPlaceholderConfigurer的类,实现解密,把解密后的信息又放回去。最后在配置DataSource时,还是用占位符${}取配置信息。

 

jdbc.properties文件内容:

Properties代码  
  1. jdbc.driverClassName = 4A490AA9B8CD7DBD61E70367C868F950541890F991000CD76A707177A0A507B9  
  2. jdbc.url = FA0DD23D31BCF4C6058626849C4611455A74B444893626DE9CF7D1E05F15586C54C098BFA29BC54A  
  3. jdbc.username = 5DE376A122083A8945FF13A1D5AFD452  
  4. jdbc.password = 5DE376A122083A8945FF13A1D5AFD452  

 

 

自定义的取加密信息的类EncryptablePropertyPlaceholderConfigurer

Java代码  
  1. public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {  
  2.     private static final String key = "0002000200020002";  
  3.   
  4.     protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)  
  5.         throws BeansException {  
  6.             try {  
  7.                 Des des = new Des();  
  8.                 String username = props.getProperty("jdbc.username");  
  9.                 if (username != null) {  
  10.                     props.setProperty("jdbc.username", des.Decrypt(username, des.hex2byte(key)));  
  11.                 }  
  12.                   
  13.                 String password = props.getProperty("jdbc.password");  
  14.                 if (password != null) {  
  15.                     props.setProperty("jdbc.password", des.Decrypt(password, des.hex2byte(key)));  
  16.                 }  
  17.                   
  18.                 String url = props.getProperty("jdbc.url");  
  19.                 if (url != null) {  
  20.                     props.setProperty("jdbc.url", des.Decrypt(url, des.hex2byte(key)));  
  21.                 }  
  22.                   
  23.                 String driverClassName = props.getProperty("jdbc.driverClassName");  
  24.                 if(driverClassName != null){  
  25.                     props.setProperty("jdbc.driverClassName", des.Decrypt(driverClassName, des.hex2byte(key)));  
  26.                 }  
  27.                   
  28.                 super.processProperties(beanFactory, props);  
  29.             } catch (Exception e) {  
  30.                 e.printStackTrace();  
  31.                 throw new BeanInitializationException(e.getMessage());  
  32.             }  
  33.         }  

 

Des.java是一个用Des算法加密和解密的工具类

Java代码  
  1. //加密  
  2. public static String Encrypt(String str, byte[] key){  
  3.     Security.addProvider(new com.sun.crypto.provider.SunJCE());  
  4.     byte[] encrypt = encryptMode(key, str.getBytes());  
  5.     return byte2hex(encrypt);  
  6. }  
  7.   
  8. //解密  
  9. public static String Decrypt(String str, byte[] key){  
  10.     Security.addProvider(new com.sun.crypto.provider.SunJCE());  
  11.     byte[] decrypt = decryptMode(key, hex2byte(str));   
  12.     return new String(decrypt);  
  13. }  

 

 

spring配置:

Xml代码  
  1. <bean id="propertyConfigurer"    
  2.       class="com.eeds.core.security.datasource.EncryptablePropertyPlaceholderConfigurer">    
  3.        <property name="locations">    
  4.            <list>    
  5.                <value>classpath:config/spring/jdbc-test.propertiesvalue>    
  6.            list>    
  7.        property>    
  8.    bean>  

 

Xml代码  
  1.   
  2. <bean id="econsoleDS" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  3.     destroy-method="close">  
  4.     <property name="driverClass">  
  5.         <value>${jdbc.driverClassName}value>  
  6.     property>                 
  7.     <property name="jdbcUrl">  
  8.         <value>${jdbc.url}value>  
  9.     property>     
  10.     <property name="user">  
  11.         <value>${jdbc.username}value>  
  12.     property>  
  13.     <property name="password">  
  14.         <value>${jdbc.password}value>  
  15.     property>  
  16.                 bean>  

你可能感兴趣的:(java)