如何解决如何覆盖默认的加密属性前缀/后缀?
Jasypt希望加密的属性以“ ENC(...)”包装。
我正在寻找一种方法,使用“ secure [...]”之类的自定义参数覆盖默认的jasypt加密属性前缀和后缀。
有没有办法做到这一点?
解决方法
如果您使用纯jasypt库,则需要编写自己的现有EncryptableProperties
版本,默认情况下,该版本仅支持ENC(
作为前缀,)
作为后缀。
public class EncryptableProperties extends Properties {
private final String prefix;
private final String suffix;
private final StringEncryptor encryptor;
public EncryptableProperties(final Properties defaults,final StringEncryptor encryptor,final String prefix,final String suffix) {
super(defaults);
this.encryptor = encryptor;
this.prefix = prefix;
this.suffix = suffix;
}
public EncryptableProperties(final Properties defaults,final StringEncryptor encryptor){
this(defaults,encryptor,"ENC(",")");
}
@Override
public String getProperty(String key) {
String value = super.getProperty(key);
return decode(value);
}
private String decode(String value) {
if (value == null) {
return value;
}
String decryptedValue = value;
if (value.startsWith(prefix) && value.endsWith(suffix)) {
int start = prefix.length();
int end = value.length() - suffix.length();
String encryptedValue = value.substring(start,end);
decryptedValue = encryptor.decrypt(encryptedValue);
}
return decryptedValue;
}
}
用法:
Properties encryptableProperties = new EncryptableProperties(properties,stringEncryptor,"CUSTOM_PREFIX(",")");
如果您使用Spring Boot集成
您可以通过使用以下两个属性来实现
-
jasypt.encryptor.property.prefix
-
jasypt.encryptor.property.suffix
默认值为:
jasypt.encryptor.property.prefix=ENC(
jasypt.encryptor.property.suffix=)
这些属性可用于以下库
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。