使用pringboot對數據庫地址進行加密的方法
1.首先,需要在springboot項目中導入依賴;
com.github.ulisesbocchio
jasypt-spring-boot-starter
2.0.0
2.依賴導入好后,在springboot中輸入代碼即可對數據庫地址進行加密;
public class EncryptionPropertyConfig {
@Bean(name="encryptablePropertyResolver")
public EncryptablePropertyResolver encryptablePropertyResolver() {
return new EncryptionPropertyResolver();
}
class EncryptionPropertyResolver implements EncryptablePropertyResolver {
@Override
public String resolvePropertyValue(String value) {
if(StringUtils.isBlank(value)) {
return value;
}
// 值以DES@開頭的均為DES加密,需要解密
if(value.startsWith("xxxxx@")) {
return resolveDESValue(value.substring(11));
}
// 不需要解密的值直接返回
return value;
}
private String resolveDESValue(String value) {
// 自定義DES密文解密
String decode="";
try{
decode = DESCode.decode(value);
}catch (Exception e){
e.printStackTrace();
System.out.println("解密失敗!:"+ value);
}
System.out.println("value :"+value +" :"+decode);
return decode;
}
}
}