【发布时间】: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>
注意我为属性占位符设置了<property name="ignoreUnresolvablePlaceholders" value="true"/>。
我已在调试器中逐步完成此操作,似乎另一个 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