開門見山直接貼上代碼
1.AESUtil加密解密工具類 import java.security.Key; import java.security.SecureRandom; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; /** * @description: AES加密工具類 * @author: maojialong * @date: 2017年11月7日 上午10:11:02 */ public class AESUtils { //實例化密鑰 private static Key key; //原始密鑰 private static String KEY_STR = "my-springmvc-2017-11-07"; //編碼 private static String CHARSETNAME = "UTF-8"; //密鑰算法 private static String KEY_ALGORITHM = "AES"; //加密-解密算法 / 工作模式 / 填充方式 private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /** * 初始化key */ static { try { KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(KEY_STR.getBytes()); kgen.init(128, random); key = kgen.generateKey(); kgen = null; } catch (Exception e) { throw new RuntimeException(e); } } /** * @description: AES對稱加密字符串,並通過Jdk自帶Base64轉換為ASCII * @author: Administrator * @date: 2017年11月7日 上午9:37:48 * @param str * @return */ public static String getEncryptString(String str) { try { byte[] bytes = str.getBytes(CHARSETNAME); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] doFinal = cipher.doFinal(bytes); return Base64.getEncoder().encodeToString(doFinal); } catch (Exception e) { throw new RuntimeException(e); } } /** * @description: 對AES加密字符串進行解密 * @author: maojialong * @date: 2017年11月7日 上午10:14:00 * @param str * @return */ public static String getDecryptString(String str) { try { byte[] bytes = Base64.getDecoder().decode(str); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] doFinal = cipher.doFinal(bytes); return new String(doFinal, CHARSETNAME); } catch (Exception e) { throw new RuntimeException(e); } } } 2.自定義配置文件解析類 import java.util.List; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * @description: 自定義AES解密 * @author: maojialong * @date: 2017年11月7日 上午10:26:50 */ public class EncodeAESPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private List<String> encodeProperties; /** * @description: 重寫convertProperty方法,通過encodeProperties判斷是否是經過AES加密 * @author: maojialong * @date: 2017年11月7日 上午10:27:24 * @param propertyName * @param propertyValue * @return * @see org.springframework.beans.factory.config.PropertyResourceConfigurer#convertProperty(java.lang.String, java.lang.String) * TODO */ @Override protected String convertProperty(String propertyName, String propertyValue) { if(encodeProperties != null && encodeProperties.contains(propertyName)) { propertyValue = AESUtils.getDecryptString(propertyValue); } return super.convertProperty(propertyName, propertyValue); } public List<String> getEncodeProperties() { return encodeProperties; } public void setEncodeProperties(List<String> encodeProperties) { this.encodeProperties = encodeProperties; } } 3.配置文件中加載配置文件 <!-- 自定義AES加密解密 --> <bean id="propertyConfigurer" class="util.EncodeAESPlaceholderConfigurer"> <!-- 配置文件地址 --> <property name="locations"> <list> <value>classpath:conf/jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> <!-- 配置需要解密的配置項 --> <property name="encodeProperties"> <array> <value>jdbc.url</value> <value>jdbc.username</value> <value>jdbc.password</value> </array> </property> </bean>