hibernate中c3p0数据源用户名密码加密

spring2 配置文件 

Java代码 
  1. "1.0"?>  
  2. "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3.   
  4.       
  5. "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  6.         "driverClass">  
  7.             oracle.jdbc.driver.OracleDriver  
  8.           
  9.         "jdbcUrl" value="jdbc:oracle:thin:@IP:端口:SID" />  
  10.         "acquireIncrement" value="1"/>  
  11.         "idleConnectionTestPeriod" value="300"/>  
  12.         "maxPoolSize" value="5"/>  
  13.         "minPoolSize" value="1"/>  
  14.         "initialPoolSize" value="1" />  
  15.         "numHelperThreads" value="3"/>  
  16.         "maxIdleTime" value="1200" />  
  17.         "acquireRetryAttempts" value="2"/>  
  18.         "preferredTestQuery" value=" select 1 from dual "/>  
  19.         "testConnectionOnCheckin" value="true"/>  
  20.         "properties" ref="dataSourceProperties"/> 
  21.       
  22.       
  23.     "dataSourceProperties" class="PropertiesEncryptFactoryBean">  
  24.         "properties">  
  25.               
  26.                 "user">xxxx加密后  
  27.                 "password">xxxxxxx加密后  
  28.               
  29.           
  30.       



2.PropertiesEncryptFactoryBean(加密类) 

Java代码 
  1. import java.util.Properties;  
  2.   
  3. import org.springframework.beans.factory.FactoryBean;  
  4.   
  5. public class PropertiesEncryptFactoryBean implements FactoryBean {  
  6.   
  7.     private Properties properties;  
  8.       
  9.     public Object getObject() throws Exception {  
  10.         return getProperties();  
  11.     }  
  12.   
  13.     public Class getObjectType() {  
  14.         return java.util.Properties.class;  
  15.     }  
  16.   
  17.     public boolean isSingleton() {  
  18.         return true;  
  19.     }  
  20.   
  21.     public Properties getProperties() {  
  22.         return properties;  
  23.     }  
  24.   
  25.     public void setProperties(Properties inProperties) {  
  26.         this.properties = inProperties;  
  27.         String originalUsername = properties.getProperty("user");  
  28.         String originalPassword = properties.getProperty("password");  
  29.         if (originalUsername != null){  
  30.             String newUsername = deEncryptUsername(originalUsername);  
  31.             properties.put("user", newUsername);  
  32.         }  
  33.         if (originalPassword != null){  
  34.             String newPassword = deEncryptPassword(originalPassword);  
  35.             properties.put("password", newPassword);  
  36.         }  
  37.     }  
  38.       
  39.     private String deEncryptUsername(String originalUsername){  
  40.         return deEncryptString(originalUsername);  
  41.     }  
  42.       
  43.     private String deEncryptPassword(String originalPassword){  
  44.         return deEncryptString(originalPassword);  
  45.     }  
  46.     //简单加密  
  47.     private String deEncryptString(String originalString){  
  48.         StringBuilder newString = new StringBuilder();  
  49.         for (int i = 0; i < originalString.length(); i++) {  
  50.             char eachChar= originalString.charAt(i);  
  51.             char newChar = (char)(eachChar + i);  
  52.             newString.append(newChar);  
  53.         }  
  54.         return newString.toString();  
  55.     }  
  56.   
  57. }  

 



ComboPooledDataSource的properties属性可被注入user,password 
只有user,password两个属性是可配置,其他属性不存也不是取自properties(具体可看C3P0源码)

 

转自:http://aguu125.iteye.com/blog/579402

 

 

 

你可能感兴趣的:(Hibernate)