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.         [color=red]<property name="properties" ref="dataSourceProperties"/>[/color]  

  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源码)  


转载于:https://my.oschina.net/farces/blog/288435

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值