我們在搭鍵一個項目的時候,有一些信息,比如數據庫的連接信息通常會放在一個配置文件中,存放的是明碼,我們可以對關鍵信息比如用戶名和密碼進行加密,就算別人獲取了也沒有用
加密後的效果
db.username = WnplV/ietfQ=
db.password = j9ZTgFZm4H7r0AteGA6A7A==
db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
一、首先創建一個加密的工具類,對信息進行加密
我用的是DES對稱加密
package com.edward.o2o.util;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DESUtils {
private static Key key;
private static String KEY_STR = "myKey";
private static String CHARSETNAME = "UTF-8";
private static String ALGORITHM = "DES";
static {
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes());
generator.init(secureRandom);
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String getEncryptString(String str) {
BASE64Encoder base64encoder = new BASE64Encoder();
try {
byte[] bytes = str.getBytes(CHARSETNAME);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return base64encoder.encode(doFinal);
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException(e);
}
}
public static String getDecryptString(String str) {
BASE64Decoder base64decoder = new BASE64Decoder();
try {
byte[] bytes = base64decoder.decodeBuffer(str);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return new String(doFinal, CHARSETNAME);
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
System.out.println(getEncryptString("root"));
System.out.println(getEncryptString("123456abCD."));
System.out.println(getEncryptString("wxd7f6c5b8899fba83"));
System.out.println(getEncryptString("665ae80dba31fc91ab6191e7da4d676d"));
}
}
通過main方法,把要加密的信息輸出到控制檯得到密文,再把對應的明文換成加密後的密文
二、創建一個PropertyPlaceholderConfigurer後置處理器,spring配置文件中會用到這個類解析解析數據庫的配置參數
package com.edward.o2o.util;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class EncryptPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private String[] encryptPropNames = { "db.username", "db.password" };
@Override
protected String convertProperty(String propertyName, String propertyValue) {
if (isEncryptProp(propertyName)) {
String decryptValue = DESUtils.getDecryptString(propertyValue);
return decryptValue;
} else {
return propertyValue;
}
}
private boolean isEncryptProp(String propertyName) {
for (String encryptpropertyName : encryptPropNames) {
if (encryptpropertyName.equals(propertyName))
return true;
}
return false;
}
}
三、把配置文件加載到spring的配置文件中
<!-- 1.配置數據庫相關參數properties的屬性:${url} -->
<!--<context:property-placeholder location="classpath:db.properties"/>-->
<bean class="com.edward.o2o.util.EncryptPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean>
class就是第二步中用於解析配置文件的類
總結:
整個過程就是利用加密的工具類對關鍵信息加密把原文替換成密文,然後在spring的配置文件中加載需要的配置文件(比如數據庫的配置文件),加載過程中PropertyPlaceholderConfigurer後置處理器會調用加密工具類中的解密文件返回明文
PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor接口的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置文 件)中的屬性值放在另一個單獨的標準java Properties文件中去。在XML文件中用${key}替換指定的properties文件中的值。這樣的話,只需要對properties文件進 行修改,而不用對xml配置文件進行修改。