Updating properties file during spring context load

1.1k views Asked by At

I want to integrate Jasypt library with spring properties mechanism.

In example tutorial here: http://www.jasypt.org/spring31.html author assumed that user performed encryption manually and then only passed encrypted value between ENC( ) placeholders to the config file. It is not enough for me.

I want to achieve given scenario:

  1. Put this line into file with properies:

    datasource.password=DEC(mysecretpassword)
    
  2. Start application

  3. Spring Framework will encrypt password (using jasypt) and replace above line with:

    datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
    

At further startups spring only decrypts encrypted value (e.g. using org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer) and file is not modified.

I suppose I need to create my own Spring's PropertyPlaceholderConfigurer but I don't see any possibility to modify file.

I can set custom org.springframework.util.PropertiesPersister to my PropertyPlaceholderConfigurer, okay, but I still don't know which file I need to update (in my spring @Configuration may be many properties locations, some properties can be override with value from another file etc.).

In other words: I need to inject my (de|en)cryption logic AFTER property resources resolving and merging, but BEFORE injecting values to another beans.

Thank you for advance for any advices how to solve this.

1

There are 1 answers

0
Hezi Schrager On

An old problem that I solve with a solution that I found in a jira that I can't find it right now. Anyway, This is a very similar solution, In that way you "tell" the fw to load your propertiesPlaceHolder file before loading your dataSource beans.