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

spring2 配置文件 

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



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

 

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页