弹簧引导未加载正确的 Jasypt 应用程序.环境属性



我正在尝试在我的Spring Boot 1.4应用程序中实现Jasypt,因为将Spring Cloud Config用于这样的小应用程序似乎有些过分了。但是,我显然不明白 Spring 引导如何确定它运行的环境,并使用适当的属性文件。 我需要加密存储的数据源属性,例如:

spring.datasource.url=jdbc:postgresql://localhost:5432/myschema
spring.datasource.username=myuser
spring.datasource.password=ENC(ZwXHbQl^8c2U)
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database=POSTGRESQL

在我的project/config/目录中,我有三个文件:

  • application.properties : 单次输入: spring.profiles.active=local
  • application-local.properties:开发的配置值,包括本地数据库凭据
  • application-test.properties:测试环境的配置值,如数据库凭据等
  • application-prod.properties:生产环境的配置值,如数据库凭据等

我通过以下方式导入Jasypt:

编译组: 'com.github.ulisesbocchio', name: 'Jasypt-spring-boot-starter', 版本: '1.7'

我运行本地 Spock/Goovy 集成测试,所以我

@ActiveProfiles("本地,测试")

但这似乎并没有拾取属性文件。
<已修复>通过添加@ActiveProfiles(["local", "test"])

我添加了/config/application.properties 文件来设置

spring.profiles.active=local jasypt.encryptor.password=

我已经查看了有关 Jasypt 如何工作的文档,因此我可以尝试了解如何为每个环境加密我的数据库凭据。 此外,我已经能够弄清楚如何加载正确的属性文件来测试加密。

更新

看起来现在正在加载正确的 *.properties 文件(感谢伟大的反馈!),但找不到数据库密码或无法解密。 我在日志中看到以下内容:

eEncryptablePropertySourcesPostProcessor : Post-processing PropertySource instances
c.u.j.c.StringEncryptorConfiguration     : String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing String Encryptor based on properties with name 'jasyptStringEncryptor'
eEncryptablePropertySourcesPostProcessor : Converting PropertySource commandLineArgs [to EncryptableEnumerablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource servletConfigInitParams [org.springframework.core.env.PropertySource$StubPropertySource] to EncryptablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource servletContextInitParams [org.springframework.core.env.PropertySource$StubPropertySource] to EncryptablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource systemProperties [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
 eEncryptablePropertySourcesPostProcessor : Converting PropertySource systemEnvironment [org.springframework.core.env.SystemEnvironmentPropertySource] to EncryptableMapPropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource random [org.springframework.boot.context.config.RandomValuePropertySource] to EncryptablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource applicationConfig: [file:./config/application-local.properties] [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource applicationConfig: [file:./config/application.properties] [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource applicationConfig: [classpath:/application.properties] [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
 .c.EncryptablePropertySourcesInitializer : Created Encryptable Property Source 'EncryptedProperties' from locations: [classpath:application.properties]
 Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWithMD5AndDES
 c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.keyObtentionIterations, using default value: 1000
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.poolSize, using default value: 1
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.providerName, using default value: SunJCE
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.saltGeneratorClassname, using default value: org.jasypt.salt.RandomSaltGenerator
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.stringOutputType, using default value: base64
j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'

根据这一行:

Property Source 'EncryptedProperties' from locations:[classpath:application.properties]

似乎我们必须明确声明要在 @EnableEncryptableProperties( ) 注释中搜索加密值的属性文件,但这似乎不需要文件或属性值的列表,也没有找到任何人说需要这样做。

对具有多个值的注释使用 {}Groovy 中不起作用,请尝试 @ActiveProfiles(["local", "test"])@ActiveProfiles(["local", "test"] as String[]) 。请参阅数组

如果您在application.properties中设置了spring.profiles.active=local,那么您不必使用它将查找@ActiveProfiles注释application-local.properties

spring.profiles.active属性遵循与其他属性相同的排序规则,最高的属性源将获胜。这意味着您可以在 application.properties 中指定活动配置文件,然后使用命令行开关替换它们。

希望这有帮助!

基于Spring文档,它应该是:@ActiveProfiles({"local", "test"})。你能试试吗?!请参阅此处的更多详细信息:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html

希望这有帮助!

在你的情况下,你神秘地尝试引导Jasypt!!在配置服务器开始从配置存储库中提取配置之前,需要 Jasypt 解密加密的属性。

bootstrap.yaml

spring.cloud.config.server.bootstrap=true
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/jamesmedice
spring.cloud.config.server.git.username=james@medici.com
spring.cloud.config.server.git.password=ENC(#################)
jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.password=superkey

@Configuration
@ConditionalOnClass(name = "org.springframework.cloud.bootstrap.BootstrapApplicationListener")
@ConditionalOnProperty(name = "spring.cloud.bootstrap.enabled", havingValue = "true", matchIfMissing = true)
public class JasyptSpringCloudBootstrapConfiguration {
     @Configuration
     @ConditionalOnProperty(name = "jasypt.encryptor.bootstrap", havingValue = "true", matchIfMissing = true)
     @Import(EnableEncryptablePropertiesConfiguration.class)
     protected static class BootstrappingEncryptablePropertiesConfiguration {
     }
}

@ConditionalOnClass('BootstrapApplicationListener')确保配置仅在基于春季云的环境中有效。 @ConditionalOnProperty("spring.cloud.bootstrap.enabled" ...) 确保情况并非如此,一旦提供了 jasypt.encryptor.bootstrap 配置以明确禁用"bootstrapping Jasypt",Jasypt 也会自动配置。

最新更新