使用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;
}
}
}