我们在Spring 4.2.5版本上。需要有一个PropertyPlaceholderConfigurer的自定义实现,基本上是在使用它们之前解密数据。这很好用。但是,我还需要能够根据属性更改此自定义实现中使用的加密机制(使用常规上下文:property-placeholder读取)。有没有办法让这个工作?
答案 0 :(得分:1)
最简单的方法是不要使用自定义PropertyPlaceholderConfigurer
,而是使用自定义DefaultPropertiesPersister
。这是以这种方式配置的:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:...</value>
<value>...</value>
</list>
</property>
<property name="propertiesPersister">
<bean class="yourPropertiesPersister"/>
</property>
...
</bean>
然后yourPropertiesPersister
需要扩展DefaultPropertiesPersister
,这会让您实施:
public void load(Properties props, InputStream is) throws IOException {
super.load(props, is);
decrypt(props);
}
@Override
public void load(Properties props, Reader reader) throws IOException {
super.load(props, reader);
decrypt(props);
}
private void decrypt(Properties props) {
// your logic here
}
对super.load(...)
的调用将加载原始属性(内容未解密)。只需根据某些属性的内容将逻辑添加到方法decrypt(props)
即可。将解密的属性添加到props
。
答案 1 :(得分:0)
如果您不介意在项目中添加一个依赖项,jasypt
库将涵盖您的用例。使它整洁的原因是它与Spring无缝集成,它有助于进一步改变加密的参数化,即使用的算法或主加密密钥的位置。
很酷的是,spring config / class注释中的代码(在其中注入与属性文件中某个键对应的值)看起来相同,就像使用纯文本属性一样。
这是一个小例子,根据他们网站上的教程(http://www.jasypt.org/spring31.html):
application.properties =&gt;您的属性文件
datasource.driver=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost/reportsdb
datasource.username=reportsUser
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
你的Spring配置(通过注释注入时效果相同)
<!-- -->
<!-- Configuration for encryptor, based on environment variables. -->
<!-- -->
<!-- In this example, the encryption password will be read from an -->
<!-- environment variable called "APP_ENCRYPTION_PASSWORD" which, once -->
<!-- the application has been started, could be safely unset. -->
<!-- -->
<bean id="environmentVariablesConfiguration"
class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
</bean>
<!-- -->
<!-- The will be the encryptor used for decrypting configuration values. -->
<!-- -->
<bean id="configurationEncryptor"
class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>
<!-- -->
<!-- The EncryptablePropertyPlaceholderConfigurer will read the -->
<!-- .properties files and make their values accessible as ${var}. -->
<!-- -->
<!-- Our "configurationEncryptor" bean (which implements -->
<!-- org.jasypt.encryption.StringEncryptor) is set as a constructor arg. -->
<!-- -->
<bean id="propertyConfigurer"
class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="locations">
<list>
<value>/WEB-INF/classes/application.properties</value>
</list>
</property>
</bean>
<!-- -->
<!-- Our datasource is configured here, in the usual way. Jasypt's -->
<!-- EncryptedPropertyPlaceholderConfigurer will make sure that the -->
<!-- ${datasource.password} file gets decrypted and the DBCP DataSource -->
<!-- will be correctly initialised. -->
<!-- -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${datasource.driver}</value>
</property>
<property name="url">
<value>${datasource.url}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
</bean>
Jasypt解析您提供的属性文件的内容,并解密ENC(
和)
之间的值。您可以在上面提供的链接中找到更多详细信息,这是一个真正的工作示例。
jasypt
的另一个有趣特性是它提供了一个command line tool
,您可以使用它来加密原始的纯文本值,您可以选择多种算法。您可以在此处找到此工具的简明文档:http://www.jasypt.org/cli.html。