How do you get property placeholders from one .properties resolved from another .properties through jasypt?

149 Views Asked by At

I have one application.properties file located elsewhere on disk which holds the password:

# c:/tmp/application.properties
some.encrypted.string=ENC(KB7EvxzOXglSbOrr8rqiyjwJZGpuQgyGfg5hHJgM/EN3VnUZhjsaq6HquG4J+A6A)

And in my resources folder I have the datasource PW with the placeholder in the application.properties:

# resources/application.properties
spring.datasource.password=${some.encrypted.string}

This works just fine in Spring Boot. However I need to do the same in Spring3.

I've set up the beans in XML, with spring.config.additional-location pointing to c:/tmp/application.properties

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsdd">

  <bean id="randomIvGenerator" class="org.jasypt.iv.RandomIvGenerator"/>
  
  <bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
    <property name="algorithm" value="PBEWithHMACSHA512AndAES_256"/>
    <property name="passwordEnvName" value="JASYPT_ENCRYPTOR_PASSWORD"/>
  </bean>
  
  <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
    <property name="config" ref="environmentVariablesConfiguration"/>
    <property name="ivGenerator" ref="randomIvGenerator"/>
  </bean>
  
  <bean id="propertyPlaceholderConfigurer"
    class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer">
    <constructor-arg ref="configurationEncryptor"/>
    <property name="searchSystemEnvironment" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="locations">
      <list>
        <value>file:${spring.config.additional-location}</value>
        <value>classpath:application.properties</value>
      </list>
    </property>
  </bean>
</beans>

If I put another bean that uses spring.datasource.password, it correctly gets the replaced and decrypted value.

But if I get the environment, it just returns null..

final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("jasypt.xml");
return ctx.getEnvironment().getProperty("spring.datasource.password");

How do I get the replaced and decrypted value out of the properties programmatically?

0

There are 0 best solutions below