开门见山直接贴上代码

.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(, 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);
}
}
} .自定义配置文件解析类
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;
}
} .配置文件中加载配置文件
<!-- 自定义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>

最新文章

  1. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q144-Q146)
  2. Spring中的IOC\DI\AOP等概念的简单学习
  3. IOS系统ipa软件包在线安装
  4. 关于Beam Search
  5. Offer_answer_with_SDP_rfc3264
  6. Linux之查看文件大小
  7. UML 小结(2)- 理论理解
  8. VirtualBox的工作原理&amp;参考网上文章
  9. MySql数据库3【优化1】表的优化
  10. 用extern关键字使程序更加清晰
  11. BCP导出导入
  12. Grub启动配置文件
  13. .net是最牛逼的开发平台没有之一
  14. java程序的内存分配
  15. String类的一些常见的获取方法(5)
  16. 在C#程序中模拟发送键盘按键消息
  17. Swift开发教程--使用Storyboard进行界面跳转
  18. [PHP] 适配器模式的日常使用
  19. 最小生成树 HDU1301 (kuskal &amp; prim)
  20. 使用git进行版本控制

热门文章

  1. Python学习day26-面向对象之小结
  2. 转载https://www.luogu.org/problemnew/solution/P1665,http://bailian.openjudge.cn/practice/2002/的新解法
  3. 2019-10-31-C#-dotnet-获取整个局域网的-ip-地址
  4. [Ceoi2010]Pin
  5. Leetcode961. N-Repeated Element in Size 2N Array重复N次的元素
  6. Windows API GetShortPathName GetLongPathName
  7. 关于python中 and 和 or 的一些特殊使用
  8. 详解Spring MVC 4常用的那些注解
  9. 二、Web Service开发(.net)
  10. java导入导出excel