当前位置: 首页 > 知识库问答 >
问题:

spring boot未加载正确的Jasypt应用程序。环境属性

阎宾实
2023-03-14

我正试图在我的Spring Boot 1.4应用程序中实现Jasypt,因为在这样的小应用程序中使用Spring Cloud Config似乎有些过头了。然而,我显然不理解Spring Boot如何确定其运行的环境,以及如何使用适当的属性文件。我需要加密存储的数据源属性,例如:

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/目录中,我有三个文件:

  • <代码>应用程序。属性:spring的单个条目。配置文件。活动=本地
  • <代码>应用程序本地。属性:为开发配置值,包括本地db凭据
  • <代码>应用测试。属性:测试环境的配置值,如db凭据等
  • application-prod.properties(应用程序-产品属性):为生产环境配置值,如db凭据等

我通过以下方式导入Jasypt:

编译组:“com.github.ulisesbocchio,名称:“jasypt spring boot starter”,版本:“1.7”

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

@ActiveProfiles(“本地,测试”)

但这似乎并没有收集到属性文件

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

spring.profiles.active=本地jasypt.encryptor.password=

我已经阅读了Jasypt的工作原理文档,因此我可以尝试并理解如何在每个环境中加密我的db凭据。此外,我还能够找出如何加载正确的属性文件来测试加密。

更新

这似乎是正确的*。现在正在加载属性文件(多亏了大量反馈!)但数据库密码要么找不到,要么无法解密。我在日志中看到以下内容:

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()注释中明确声明要搜索哪些属性文件以搜索加密值,但这似乎并没有包含文件或属性值的列表,我也没有发现有人说需要这样做。

共有3个答案

弓方伟
2023-03-14

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

希望这有帮助!

潘胤
2023-03-14

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

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

希望这有帮助!

汤兴生
2023-03-14

使用带有多个值的注释在Groovy中不起作用,请尝试使用ActiveProfiles([“local”,“test”)或ActiveProfiles([“local”,“test”]作为字符串[])。请参见阵列

 类似资料:
  • 我正在运行一个springboot应用程序,它需要信任我在本地信任库中添加的证书。现在我将它设置在intellij中的运行配置选项下,它有效。ex- 我想知道有没有办法从应用程序设置它。以我们设置spring属性的方式在springboot中创建属性文件?

  • 在我的项目中,我想使用特定于环境的属性文件。例如,如果我在开发中运行它,它应该使用应用程序。dev.properties,对于生产,它应该使用应用程序。产品属性等等。 我有下面两个文件在我的资源文件夹。 application.properties(用于生产) application.dev.properties(用于开发) 我有一个属性像下面的每个文件。 为了刺激 给德夫 我有一门课,如下所示

  • 我试图使用Micronaut 3加载特定于环境的属性。我有两个文件和 application.yml application-dev.yml 正在尝试访问URL 在teamConfiguration变量中,我可以看到文件不是来自文件。 正如我所知,默认情况下Micronaut只查找应用程序。yml。然后,对于测试、开发和产品,它加载应用程序。yml并用应用程序测试中定义的值覆盖其中的任何值。yml

  • 我正在一个spring启动的应用程序,我需要你的协助在下面的场景。 我有每个环境的属性文件,比如application-dev.properties,application-prod.properties等。我的应用程序可以通过使用spring@profile注释来加载特定于环境的属性文件。 请帮帮忙。

  • 我有一个quarkus应用程序,当我用下面的命令构建应用程序时,进程第一次开始完美地编译quarkus:dev-DskipTests=true 成功启动的日志: 但是当我停下来重新开始这个过程时,过程并没有开始... 启动失败的日志: 当我尝试重新启动机器,然后启动quarkus服务时,它会再次工作。 pom。xml: 如果有人遇到过类似的行为,请告诉我,这可能是什么根源。

  • 我有一个小的Flask应用程序,它使用python dotenv库加载我存储在名为的文件中的环境变量。一切都很好,但我最近意识到我实际上从未调用方法,所以我不确定它是如何工作的。出于好奇,我试着绕过它: 我甚至删除了导入()-仍然有效 我重命名了<代码>。env到-不工作 我用