數據源的加密解密

先來看一下數據庫配置文件:java

dbCustomer.driverClass=org.gjt.mm.mysql.Driver
dbCustomer.jdbcUrl=jdbc:mysql://192.168.1.81:3306/p2p_customer?useUnicode=true&characterEncoding=UTF8
dbCustomer.user=PCVoqoFQn5w=     加密後的用戶名
dbCustomer.password=eh1IPqyJjLs=  加密後的密碼
#
dbCustomer.initialPoolSize=10
dbCustomer.maxIdleTime=60
dbCustomer.maxPoolSize=50
dbCustomer.minPoolSize=10
#
dbCustomer.acquireIncrement=3
dbCustomer.acquireRetryDelay=1000
dbCustomer.acquireRetryAttempts=30
dbCustomer.breakAfterAcquireFailure=false

applicationContext.xml 中的C3P0中的配置以下:mysql

 <!-- 數據庫鏈接池管理 -->
    <bean id="c3p0DataSourceCustomer" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${dbCustomer.driverClass}"/>
        <property name="jdbcUrl" value="${dbCustomer.jdbcUrl}"/>
        <!-- <property name="user" value="${dbCustomer.user}"/>
        <property name="password" value="${dbCustomer.password}"/> -->
        <property name="properties" ref="dataSourcePropertiesCustomer"/> 

        <property name="initialPoolSize" value="${dbCustomer.initialPoolSize}"/>
        <property name="maxIdleTime" value="${dbCustomer.maxIdleTime}"/>
        <property name="maxPoolSize" value="${dbCustomer.maxPoolSize}"/>
        <property name="minPoolSize" value="${dbCustomer.minPoolSize}"/>
        <property name="acquireIncrement" value="${dbCustomer.acquireIncrement}"/>
        <property name="acquireRetryDelay" value="${dbCustomer.acquireRetryDelay}"/>
        <property name="acquireRetryAttempts" value="${dbCustomer.acquireRetryAttempts}"/>
        <property name="breakAfterAcquireFailure" value="${dbCustomer.breakAfterAcquireFailure}"/>
    </bean>
<bean id="dataSourcePropertiesCustomer" class="com.hzfh.service.EncryptedDataSourceFactory"> <property name="properties"> <props> <prop key="user">${dbCustomer.user}</prop> <prop key="password">${dbCustomer.password}</prop> </props> </property> </bean>

項目啓動加載時,會自動找到 com.hzfh.service.EncryptedDataSourceFactory 類,而且 用戶名:user、密碼:password 傳入到該類中進行解密算法

 1 package com.hzfh.service.EncryptedDataSourceFactory;
 2 import java.io.UnsupportedEncodingException;
 3 import java.util.Properties;
 4 
 5 import org.springframework.beans.factory.FactoryBean;
 6 
 7 import com.hzframework.encrypt.DESEncoder;
 8 import com.hzframework.encrypt.Encoder;
 9   
10 public class EncryptedDataSourceFactory implements FactoryBean {  
11   
12     private Properties properties;  
13       
14     public Object getObject() throws Exception {  
15         return getProperties();  
16     }  
17   
18     public Class getObjectType() {  
19         return java.util.Properties.class;  
20     }  
21   
22     public boolean isSingleton() {  
23         return true;  
24     }  
25   
26     public Properties getProperties() {  
27         return properties;  
28     }  
29   
30     public void setProperties(Properties inProperties) {  
31         this.properties = inProperties;  
32         String originalUsername = properties.getProperty("user");  
33         String originalPassword = properties.getProperty("password");  
34         if (originalUsername != null){  
35             String newUsername = decryptDESUsername(originalUsername);  
36             properties.put("user", newUsername);  
37         }  
38         if (originalPassword != null){  
39             String newPassword = decryptDESPassword(originalPassword);  
40             properties.put("password", newPassword);  
41         }  
42     }  
43       
44     private String decryptDESUsername(String originalUsername){  
45         return decryptDES(originalUsername);  
46     }  
47       
48     private String decryptDESPassword(String originalPassword){  
49         return decryptDES(originalPassword);  
50     }  
51     /**
52      * 解密
53      * @param data 原始數據
54      * @return 加密後的數據
55      */
56     public  String decryptDES(String data) {
57         try {
58             String key = "GWWEEuUvhV4=";
59             byte[] bytes = Encoder.decryptBASE64(data);
60             return new String(DESEncoder.decrypt(bytes, key));
61         } catch (Exception e) {
62             e.printStackTrace();
63         }
64         return null;
65     }
66     /**
67      * 加密
68      * @param data 原始數據
69      * @return 加密後的數據
70      */
71     public  String encryptDES(String data) {
72         try {
73             String key ="GWWEEuUvhV4=";
74             byte[] bytes = toByteArray(data);
75             return Encoder.encryptBASE64(DESEncoder.encrypt(bytes, key));
76         } catch (Exception e) {
77             e.printStackTrace();
78         }
79         return null;
80     }
81     private  byte[] toByteArray(String str) throws UnsupportedEncodingException {
82         return str.getBytes("UTF-8");
83     }
84   
85 }  

上述com.hzfh.service.EncryptedDataSourceFactory類須要繼承FactoryBean ,同時裏面的加密、解密算法就要根據本身項目中的加密解密去寫了,能夠參考上一篇文章spring

對用戶名、密碼加密時,我用到了單元測試 直接生產加密後的字符串sql

 1     @Test
 2     public void getEncrypt(){
 3             try {
 4                 String key ="GW0EYuUvhV4=";
 5                 byte[] bytes = toByteArray("123456");
 6                 System.out.println(Encoder.encryptBASE64(DESEncoder.encrypt(bytes, key)));
 7             } catch (Exception e) {
 8                 e.printStackTrace();
 9             }
10     }
11     private  byte[] toByteArray(String str) throws UnsupportedEncodingException {
12         return str.getBytes("UTF-8");
13     }

輸出:eh1IPqyJjLs=數據庫

這樣數據源就能夠進行密文顯示了,同時不影響數據庫的鏈接。app

相關文章
相關標籤/搜索