【问题标题】:Spring throws exception trying to resolve property placeholder using a Jasypt encrypted property placeholderSpring 抛出异常尝试使用 Jasypt 加密的属性占位符解析属性占位符
【发布时间】:2015-01-16 12:42:49
【问题描述】:

我正在尝试使用 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>

注意我为属性占位符设置了&lt;property name="ignoreUnresolvablePlaceholders" value="true"/&gt;

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

奇怪的是,它工作得很好——我不知道是什么改变了它。

很确定正在找到道具文件“credentials.properties” - EncryptablePropertyPlaceholderConfigurer 没有抱怨它。它肯定具有其中定义的属性 my.user 。甚至 Intellij 也在编辑器中进行替换!

旁注,不认为它是相关的,但是这个 spring 上下文是通过 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)

【问题讨论】:

    标签: java spring spring-mvc servlets jasypt


    【解决方案1】:

    whelp,这解决了它:

    改变了:

    <context:property-placeholder/>
    

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

    【讨论】:

    • 这个答案的简单性非常有效。我只有一个补充:就我而言,我还必须将 order="1" 属性添加到属性占位符,以便处理 web.xml 中的上下文参数。
    • 在我的情况下(“我在属性文件中使用了 ENC() 但没有被 jasypt 加密”)只需设置 propertyPlaceholderConfigurer.setOrder(1) 即可解决问题。
    【解决方案2】:
    <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" />
    

    【讨论】:

    • 试一试——同样的错误。就像我说的,我不认为 EncryptablePropertyPlaceholderConfigurer 在查找凭据时遇到问题。属性。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2014-07-16
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多