在jasypt EncryptablePropertyPlaceholderConfigurer中修剪尾随空格

时间:2013-08-07 18:26:21

标签: java eclipse spring jasypt

我们正在使用Spring和jasypt EncryptablePropertyPlaceholderConfigurer来阅读application.properties文件。

如果某些属性值最后包含空格,有时会发生问题,在使用@Value(${})标记读取值时,我们最后也会得到尾随空格,从而产生问题。

现在类EncryptablePropertyPlaceholderConfigurer是最终的,所以无法扩展,我搜索了很多,以确定在修剪字符串值周围的空格后是否有任何方法来获取属性。

有人可以建议如何处理这种情况吗?

2 个答案:

答案 0 :(得分:1)

您可以使用构造函数中传递的自定义StringEncryptor创建EncryptablePropertyPlaceholderConfigurer。在这个CustomStringEncryptor.decrypt()中你做trim()。 (在这种情况下,您不知道要解密的属性是哪个)

你可以绕过最后的委托:

class CustomStringEncryptor implements StringEncryptor{
  private StringEncryptor delegate;

  public CustomStringEncryptor(StandardPBEStringEncryptor delegate){
    this.delegate = delegate;
  }

  String decrypt(String encryptedMessage){
    String message = this.delegate.decrypt(encryptedMessage);
    if(null != message) message = message.trim();
    return message;
  }
}

答案 1 :(得分:0)

所以我在“bellabax”的帮助下找到了我的问题的答案 我覆盖了属性persister并实现了我自己的方法

propertyConfigurator.setPropertiesPersister(new MyDefaultPropertiesPersister());

    @Override
    public void load(Properties props, InputStream is) throws IOException {

    props.load(is);
    for (Entry<Object, Object> property : props.entrySet()) {
        property.setValue(property.getValue().toString().trim());
    }
}

现在我的属性被剪裁为尾随空格 我希望这对某人有所帮助。

相关问题