spring 加密jdbc连接的用户名和密码或者其他properties方法
来源:互联网 发布:mysql match against 编辑:程序博客网 时间:2024/05/21 14:58
1.编写一个类EncrypPropertyPlaceholderConfigurer继承PropertyPlaceholderConfigurer 重写processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) 方法。具体代码如下:
package xx.aes;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class EncrypPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
String username=props.getProperty("jdbc.username");
if(username!=null){//将加密的username解密后塞到props
props.setProperty("jdbc.username", AESCoderUtils.AESDncode(username));
}
String password=props.getProperty("jdbc.password");
if(username!=null){
props.setProperty("jdbc.password", AESCoderUtils.AESDncode(password));
}
super.processProperties(beanFactoryToProcess, props);
}
}
其中AESCoderUtils为加解密工具类,具体代码如下,使用AES加密
package xx.aes;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import com.set.direwolf.sys.utils.Constants;
public class AESCoderUtils {
/*
* 加密
*/
public static String AESEncode(String content) {
try {
// 1.构造密钥生成器,指定为AES算法,不区分大小写
KeyGenerator keygen = KeyGenerator.getInstance("AES");
// xxx 为密钥
keygen.init(128, new SecureRandom("xxx".getBytes()));
SecretKey original_key = keygen.generateKey();
byte[] raw = original_key.getEncoded();
SecretKey key = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] byte_encode = content.getBytes("utf-8");
byte[] byte_AES = cipher.doFinal(byte_encode);
String AES_encode = new String(Base64.encodeBase64(byte_AES));
return AES_encode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/*
* 解密
*/
public static String AESDncode(String content) {
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128, new SecureRandom("xxx".getBytes()));
SecretKey original_key = keygen.generateKey();
byte[] raw = original_key.getEncoded();
SecretKey key = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] byte_content = Base64.decodeBase64(content);
byte[] byte_decode = cipher.doFinal(byte_content);
String AES_decode = new String(byte_decode, "utf-8");
return AES_decode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}
2.将<context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />改为
<bean class="xx.aes.EncrypPropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"/>
</bean>
3.jdbc.properties 里面 jdbc.username,jdbc.password 为加密后的值。
参考:http://www.cnblogs.com/liunanjava/p/4297854.html
- spring 加密jdbc连接的用户名和密码或者其他properties方法
- PropertyPlaceholderConfigurer扩展,隐藏jdbc.properties中的用户名、密码、连接地址
- 使用properties属性存储连接数据库用到的用户名和密码
- 加密jdbc配置文件中的用户名密码
- MyEclipse SVN插件 去掉或者修改以前记住的用户名和密码方法
- MyEclipse SVN插件 去掉或者修改以前记住的用户名和密码方法
- JDBC用户名和密码登录
- 清除sql密码及获得asp连接SQL默认的用户名和密码的方法
- Spring+mysql+druid对数据库连接的用户名密码加密
- Phoenix Framework中为数据库用户名和密码加密的方法
- 清除记住局域网其他机子的用户名和密码
- spring websocket stomp 连接开启了用户名和密码认证的ActiveMQ
- spring 数据库用户名密码加密解密
- Spring配置JNDI加密用户名密码
- 用户名和密码的测试方法
- SVN-用户名和密码的清除方法
- TortoiseGit 记住用户名和密码的方法
- TortoiseGit 记住用户名和密码的方法
- 非Number 类型的数字相加转换 千位数添加逗号,等问题
- AssetBundle资源冗余检测
- 编译Gerrit及其插件的例子
- Tensorflow 多线程设置
- JS判断字符串中是否含有某个字符的方法
- spring 加密jdbc连接的用户名和密码或者其他properties方法
- 两数组的交2
- 为什么一直跳转这个页面
- 每天学点Linux之解压打包
- MYIR Zturn Vivado工程更新IP核
- MYSQL字符类型
- linux进程间通信-有名管道(FIFO)
- 一位资深程序员大牛给予Java初学者的学习建议
- TensorFlow 队列与多线程的应用