Spring-boot:如何从class路径或文件系统加载钥匙店资源文件?

发布于 2025-01-30 11:17:11 字数 283 浏览 2 评论 0原文

我的Spring-Boot应用程序需要通过Java密钥库访问私钥。对于测试,我想使用一个在类Path上可用的测试密钥库(因此我可以轻松地执行应用程序作为集成测试的一部分),而在生产中,我想使用一些外部卷中的密钥库(例如,例如,安装在容器中作为K8秘密)。

我知道Spring-Boot @Value注释使我可以注入资源。但是,我看不出如何使尊重春季环境(例如,测试或生产)的动态动态。 Spring-boot是否提供了解决上述问题的开箱即用途方式,还是我需要编写自己的Bean来根据部署环境加载正确的密钥库?

My spring-boot application requires access to private keys via a Java keystore. For testing, I would like to use a test keystore that is available on the classpath (so I can easily execute the application as part of an integration test), while in production I would like to use a keystore from some external volume (e.g., mounted inside a container as k8 secret).

I know that the spring-boot @Value annotation allows me to inject a resource. However, I don't see how it can be made dynamic to respect the spring environment (e.g., test or production). Does spring-boot provide an out-of-the-box way to solve the above problem, or do I need to write my own bean to load the correct keystore depending on the deployment environment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2025-02-06 11:17:11

您可以将属性用于资源路径,并根据活动配置文件以不同的方式设置应用程序中的属性值。

@Value("${my.resource.path:#{null}}")
private Resource myResource;

application.yml:

spring:
    config:
        activate:
            on-profile: test
my.resource.path: 'classpath:test/path/keystore.jks'

---
spring:
    config:
        activate:
            on-profile: prod
my.resource.path: 'file:prod/path/keystore.jks'

You can use a property for the resource path and set that property values in your application props differently based on an active profile.

@Value("${my.resource.path:#{null}}")
private Resource myResource;

application.yml:

spring:
    config:
        activate:
            on-profile: test
my.resource.path: 'classpath:test/path/keystore.jks'

---
spring:
    config:
        activate:
            on-profile: prod
my.resource.path: 'file:prod/path/keystore.jks'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文