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


阅读全文
1 0