我试图让jasypt解密(以前加密的)属性值,最终将用于登录到数据库。解密工作正常,除了我介绍Maven配置文件时。我有一个本地的/ dev/prod属性文件,这是特定于环境的。Jasypt加密不与Maven配置文件配合使用
这是我的spring3配置的相关部分。这是代码示例中最关键的部分:这是推动如何设置解密以及将解密字符串设置为示例虚拟bean的方式。
<bean id="jvmVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentPBEConfig"
p:password="secret_password_here"/>
<bean id="jvmConfigurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"
p:config-ref="jvmVariablesConfiguration"/>
<bean id="jvmPropertyConfigurer" class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer"
p:locations-ref="passwordProps">
<constructor-arg ref="jvmConfigurationEncryptor"/>
</bean>
<util:list id="passwordProps">
<value>classpath:database.properties</value>
</util:list>
<encryption:encryptable-properties id="dbProps" encryptor="jvmConfigurationEncryptor" location="classpath:database.properties"/>
<bean id="dummy" class="DummyPropertyTest">
<property name="prop" value="${database.bar}"/>
</bean>
在我的一个maven poms中,这里是我指定配置文件的地方。
...
<profiles>
<profile>
<id>local</id>
<properties>
<build.profile.id>local</build.profile.id>
</properties>
<build>
<filters>
<filter>src/main/resources/properties/${build.profile.id}/database.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</profile>
<!--dev and prod profiles follow this in a similar pattern -->
....
我使用jasypt 1.9.1版本:
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt-spring3</artifactId>
<version>1.9.1</version>
</dependency>
我有一个主数据库属性文件(database.properties)安装在/ src目录/主/资源,它具有这些特性的占位符:
database.url=${database.url}
database.username=${database.username}
database.password=${database.password}
database.dialect=${database.dialect}
database.driver=${database.driver}
database.show_sql=${database.show_sql}
database.bar=${database.bar}
然后这里是我的本地属性文件,位于/src/main/resources/properties/local/database.properties:
database.url=jdbc:hsqldb:hsql://localhost/db
database.username=sa
database.password=
database.dialect=MyHSQLDialect
database.driver=org.hsqldb.jdbcDriver
database.show_sql=true
database.bar=ENC(RSuprdBgcpdheiWX0hJ45Q==)
这里是我的样本spring bean代码,只是读取它设置的属性。如果一切正常,该值将被打印到stdout解密。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DummyPropertyTest {
private String prop;
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
@Value("#{dbProps['database.bar']}")
public String otherProp;
public String getOtherProp() {
return otherProp;
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext-common.xml");
DummyPropertyTest dpt = (DummyPropertyTest) ctx.getBean("dummy");
System.out.println("what's my property being set??: "+dpt.getProp());
System.out.println("otherProp:"+dpt.getOtherProp());
}
}
如果我调整我的春天配置驻留在通常只包含了每个属性的环境重写只是占位符的主属性文件的属性来读取,然后解密工作。但解密不起作用试图从本地属性文件读取加密的属性。我试图调整弹簧配置,希望这可能只是一个类路径问题,但这似乎也没有帮助。
我是否需要重写Spring如何查看属性前缀和后缀,如果仅用于可能需要加密的属性? (如果我这样做,那么这似乎适用于所有属性,而不仅仅是可加密属性,因为Jasypt的EncryptablePropertyPlaceholderConfigurer是Spring的PropertyPlaceholderConfigurer的替代替代品)。
这里是程序的输出,如果我有两个属性文件设置为我说明: what's my property being set??: ENC(RSuprdBgcpdheiWX0hJ45Q==)
这里是程序的输出,如果我有主属性文件包含加密属性: what's my property being set??: sa
我不确定问题是Spring还是Jayspt。我不认为这是Maven。如果可能的话,我宁愿不放弃现在的Maven配置文件。
为清晰起见,运行时示例进行了编辑。
* 更新 *:我可以确认,如果我使用Jasypt Spring配置方式 <encryption:encryptable-properties id="dbProps" encryptor="jvmConfigurationEncryptor" location="classpath:database.properties"/>
然后在我的测试豆我可以连线了一个成员有值正确解密分配给它的财产:
@Value("#{dbProps['database.bar']}")
public String otherProp;
这似乎工作。但我真的需要PropertyOverride的东西来工作,以便我可以正确地获取数据库配置。