【发布时间】:2014-06-27 11:49:52
【问题描述】:
我正在使用 Spring Boot 创建一个访问数据库的简单 Web 应用程序。我通过在application.properties 中设置spring.datasource.* 属性来利用数据源的自动配置功能。这一切都非常出色,而且速度非常快 - @Spring 的工作人员很棒!
我公司的政策是不应该有明文密码。因此我需要对sping.datasource.password 进行加密。经过一番挖掘,我决定创建一个 org.springframework.boot.env.PropertySourceLoader 实现,它创建一个 jasypt org.jasypt.spring31.properties.EncryptablePropertiesPropertySource,如下所示:
public class EncryptedPropertySourceLoader implements PropertySourceLoader
{
private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
public EncryptedPropertySourceLoader()
{
//TODO: this could be taken from an environment variable
this.encryptor.setPassword("password");
}
@Override
public String[] getFileExtensions()
{
return new String[]{"properties"};
}
@Override
public PropertySource<?> load(final String name, final Resource resource, final String profile) throws IOException
{
if (profile == null)
{
final Properties props = PropertiesLoaderUtils.loadProperties(resource);
if (!props.isEmpty())
{
return new EncryptablePropertiesPropertySource(name, props, this.encryptor);
}
}
return null;
}
}
然后我用META-INF/spring.factories 文件将它打包到它自己的jar 中,如下所示:
org.springframework.boot.env.PropertySourceLoader=com.mycompany.spring.boot.env.EncryptedPropertySourceLoader
当使用 mvn spring-boot:run 从 maven 运行时,这非常有效。当我使用java -jar my-app.war 将其作为独立战争运行时,就会出现问题。当我尝试连接到数据库时,应用程序仍然加载但失败,因为密码值仍然是加密的。添加日志记录显示EncryptedPropertySourceLoader 从未加载。
对我来说,这听起来像是一个类路径问题。在 maven 下运行时,jar 加载顺序是严格的,但是一旦在嵌入 tomcat 下,就没有什么可以说我的自定义 jar 应该在 Spring Boot 之前加载。
我尝试将以下内容添加到我的 pom.xml 以确保保留 classpth,但它似乎没有任何效果。
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
有人有什么想法吗?提前致谢。
更新:
向前迈出一步:我已经设法通过让EncryptedPropertySourceLoader 类实现org.springframework.core.PriorityOrdered 接口并从getOrder() 返回HIGHEST_PRECEDENCE 来解决这个问题。这现已解决了 PropertySourceLoader 未被使用的问题。但是,它现在在尝试解密属性时抛出以下错误:
org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: PBEWithMD5AndDES SecretKeyFactory not available
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:716)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705)
at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
at org.jasypt.properties.EncryptableProperties.decode(EncryptableProperties.java:230)
at org.jasypt.properties.EncryptableProperties.get(EncryptableProperties.java:209)
at org.springframework.core.env.MapPropertySource.getProperty(MapPropertySource.java:36)
at org.springframework.boot.env.EnumerableCompositePropertySource.getProperty(EnumerableCompositePropertySource.java:49)
at org.springframework.boot.context.config.ConfigFileApplicationListener$ConfigurationPropertySources.getProperty(ConfigFileApplicationListener.java:490)
同样,从mvn spring-boot:run 运行时不会发生这种情况,但从可执行的war 文件运行时会发生这种情况。两种场景都使用相同的 JVM (jdk1.6.0_35)。 Google/Stackoverflow 上的结果表明这是 java 安全策略的一个问题,但是当它从 maven 运行时它确实有效,我想我可以打折扣。可能是包装问题...
【问题讨论】:
-
spring.factories在WEB-INF/lib的罐子里? -
是的。打开从 maven 生成的 WAR 会得到
my-app.war!WEB-INF/lib/utility.jar!META-INF/spring.factories -
我想这是一个类加载器问题。它可以在 JAR(而不是 WAR)中工作吗?
-
查看更新:它现在使用自定义 PropertySourceLoader(因为它现在实现了 PriorityOrdered),但仍然面临一些我怀疑与 maven 与可执行 jar 的差异有关的问题。
-
它可以在 JAR 中工作吗?
标签: spring-boot jasypt