加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

c3p0数据源用户名密码加密或解密

(2013-11-25 18:03:23)
标签:

c3p0

加密

用户名密码

it

分类: ssh
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 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 0originalString.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. }  

自己实验的时候,总是提示密码错误,以为自己是哪一步错了,后来才发现,只要将
<property name="properties" ref="dataSourceProperties"/> 挪到最前面就可以了
估计是spring读的顺序有限制吧,

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有