一般我们都会在SSM项目中使用明文方式进行数据库的访问,然而这样做的方式不安全,容易泄露个人的信息
一般的jdbc的配置是这样的:
jdbc.username=root
jdbc.password=123456
我们可以数据库的用户名和密码进行加密,这样就能大大增加系统的安全性
例如:
encrypt.jdbc.username=uKXp9J7+X8lEB0KRJrK5oQ==
encrypt.jdbc.password=N1B3Nleg/d7IbThNCKH0Xw==
那么怎样才能实现呢?
我们可以利用Spring来进行配置
首先我们需要一套加密解密的算法
package Study.Tools;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class SecretKit {
public static String enc(String encodeRules,String content){
try {
KeyGenerator keygen=KeyGenerator.getInstance("AES");
keygen.init(128, new SecureRandom(encodeRules.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(new BASE64Encoder().encode(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 dec(String encodeRules,String content){
try {
KeyGenerator keygen=KeyGenerator.getInstance("AES");
keygen.init(128, new SecureRandom(encodeRules.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= new BASE64Decoder().decodeBuffer(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;
}
public static void main(String[] args) {
String rules = "aksdfjalsdfjlkasjdflasdf";
System.out.println("-----加密----");
String enc = enc(rules,"123456");
System.out.println(enc);
System.out.println("------解密----");
String dec = dec(rules, "N1B3Nleg/d7IbThNCKH0Xw==");
System.out.println(dec);
}
}
我们可以测试一下加密的结果:


接下来我们进行将密文配置进去
直接定义一个类DecryptPropertyPlaceholderConfigurer继承PropertyPlaceholderConfigurer类,重写converProperty方法,并编写一个方法判断属性是否需要扫描
public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
@Override
protected String convertProperty(String propertyName,String propertyValue){
String decValue = propertyValue;
try {
if(isEncryptPropertyVal(propertyName)){
decValue = SecretKit.dec(propertyValue);
}
} catch (Exception e) {
e.printStackTrace();
}
return decValue;
}
private boolean isEncryptPropertyVal(String propertyName){
if(propertyName.startsWith("encrypt")){
return true;
}else{
return false;
}
}
}
写好类后我们直接去Spring对该类进行配置(注意:需要在数据源项之前引入)
<bean class="cn.qblank.Util.DecryptPropertyPlaceholderConfigurer">
<property name="location" value="classpath:datasource.properties"></property>
</bean>
移除原本的明文,改用加载现有的密文
最后不要忘记修改连接的配置文件
 |