先来看一下数据库配置文件:
dbCustomer.driverClass=org.gjt.mm.mysql.DriverdbCustomer.jdbcUrl=jdbc:mysql://192.168.1.81:3306/p2p_customer?useUnicode=true&characterEncoding=UTF8dbCustomer.user=PCVoqoFQn5w= 加密后的用户名
dbCustomer.password=eh1IPqyJjLs= 加密后的密码#dbCustomer.initialPoolSize=10dbCustomer.maxIdleTime=60dbCustomer.maxPoolSize=50dbCustomer.minPoolSize=10#dbCustomer.acquireIncrement=3dbCustomer.acquireRetryDelay=1000dbCustomer.acquireRetryAttempts=30dbCustomer.breakAfterAcquireFailure=false
applicationContext.xml 中的C3P0中的配置如下:
${dbCustomer.user} ${dbCustomer.password}
项目启动加载时,会自动找到 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 ,同时里面的加密、解密算法就要根据自己项目中的加密解密去写了,可以参考上一篇文章
对用户名、密码加密时,我用到了单元测试 直接生产加密后的字符串
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=
这样数据源就可以进行密文显示了,同时不影响数据库的连接。