Spring尝试使用Jasypt加密属性占位符解析属性占位符时抛出异常

时间:2014-11-19 05:06:31

标签: java spring spring-mvc servlets jasypt

我正在尝试使用Jasypt' EncryptablePropertyPlaceholderConfigurer加载加密属性。

这是我的应用程序上下文,包含违规bean和加密属性占位符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>

注意我为属性占位符设置了<property name="ignoreUnresolvablePlaceholders" value="true"/>

我已在调试器中逐步完成此操作,似乎另一个Property Placeholder实例来自某个地方,并且决定$ {my.user}未在任何地方设置并抛出异常。

奇怪的是,这工作得很好 - 我不知道我改变了什么打破了这个。

非常确定prop文件&#34; credentials.properties&#34;正在被发现 - EncryptablePropertyPlaceholderConfigurer并没有抱怨它。它肯定有my.user定义的属性。甚至Intellij也在编辑器中进行替换!

旁注,不要认为它是相关的,但是这个春天的上下文是通过Jersey 2 servlet上下文加载的。

以下是例外:

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 个答案:

答案 0 :(得分:2)

whelp,这解决了它:

改变:

<context:property-placeholder/>

<context:property-placeholder ignore-unresolvable="true"/>

答案 1 :(得分:1)

<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" />