Spring throws exception trying to resolve property placeholder using a Jasypt encrypted property placeholder

4.7k views Asked by At

I am trying to load encrypted properties using Jasypt's EncryptablePropertyPlaceholderConfigurer.

Here is my application context, with the offending bean and the encrypted property placeholder bean:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder/>

    <bean class="com.blahblah.OffendingBean">
        <property name="user" value="${my.user}"/>
        <property name="password" value="${my.password}"/>
    </bean>

    <bean id="propertyConfigurer"
          class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>credentials.properties</value>
            </list>
        </property>
    </bean>

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration"/>
    </bean>

    <bean id="environmentVariablesConfiguration"
          class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES"/>
        <property name="password" value="not telling you"/>

    </bean>
</beans>

Note I have <property name="ignoreUnresolvablePlaceholders" value="true"/> set for the property placeholder.

I have stepped through this in the debugger, and it seems like another Property Placeholder instance is coming from somewhere and deciding ${my.user} is not set anywhere and throws an exception.

The weird thing is this was working just fine - I do not know what I changed that broke this.

Pretty sure the prop file "credentials.properties" is being found - EncryptablePropertyPlaceholderConfigurer is not complaining about it. It definitely has the property my.user defined in it. Even Intellij is doing the substitution in the editor!

side note, don't think it is relevant, but this spring context is being loaded via a Jersey 2 servlet context.

Here is the exception:

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'com.blahblah.OffendingBean#0' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'my.user' in string value "${my.user}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at org.glassfish.jersey.server.spring.SpringComponentProvider.createXmlSpringConfiguration(SpringComponentProvider.java:164)
    at org.glassfish.jersey.server.spring.SpringComponentProvider.createSpringContext(SpringComponentProvider.java:155)
    at org.glassfish.jersey.server.spring.SpringComponentProvider.initialize(SpringComponentProvider.java:98)
2

There are 2 answers

2
marathon On BEST ANSWER

whelp, this fixed it:

changed:

<context:property-placeholder/>

to

<context:property-placeholder ignore-unresolvable="true"/>
2
sunysen On
<bean id="propertyConfigurer"
          class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <!-- change it -->
                <value>classpath:credentials.properties</value>
            </list>
        </property>
 </bean>
 <!-- add it -->
 <context:property-placeholder location="classpath:jdbc.properties" />