数据源的加密解密
先來看一下數(shù)據(jù)庫配置文件:
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=falseapplicationContext.xml 中的C3P0中的配置如下:
<!-- 數(shù)據(jù)庫連接池管理 --><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>
項(xiàng)目啟動(dòng)加載時(shí),會(huì)自動(dòng)找到?com.hzfh.service.EncryptedDataSourceFactory 類,并且 用戶名:user、密碼:password 傳入到該類中進(jìn)行解密
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 原始數(shù)據(jù) 54 * @return 加密后的數(shù)據(jù) 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 原始數(shù)據(jù) 69 * @return 加密后的數(shù)據(jù) 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 ,同時(shí)里面的加密、解密算法就要根據(jù)自己項(xiàng)目中的加密解密去寫了,可以參考上一篇文章
對(duì)用戶名、密碼加密時(shí),我用到了單元測(cè)試 直接生產(chǎn)加密后的字符串
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=
這樣數(shù)據(jù)源就可以進(jìn)行密文顯示了,同時(shí)不影響數(shù)據(jù)庫的連接。
?
轉(zhuǎn)載于:https://www.cnblogs.com/benefitworld/p/5843081.html
總結(jié)
- 上一篇: fir.im Weekly - 做一款
- 下一篇: 《转载》POI导出excel日期格式