Question

We are using Spring along with jasypt EncryptablePropertyPlaceholderConfigurer to read the application.properties file.

The problem sometimes happen if some of the properties value contain white-spaces in the end, on reading the value using the @Value(${}) tag, we get the trailing space also in the end which creates problems.

Now the class EncryptablePropertyPlaceholderConfigurer is final so can't be extended and I searched a lot to figure out if any way to get the properties after trimming of the white-spaces around the string value.

Can anybody suggest how to handle this scenario?

Was it helpful?

Solution 2

So i found the answer to my problem with help from "bellabax" I overrid the Properties persister and implemented my own method

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());
    }
}

Now my properties are trimmed of trailing space I hope this helps some one.

OTHER TIPS

You can create a EncryptablePropertyPlaceholderConfigurer with custom StringEncryptor passed in constructor. In this CustomStringEncryptor.decrypt() do your trim(). (In this case you don't know which is the property you are decrypting)

You can bypass final with delegation:

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;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top