【问题标题】:Spring boot app access to Azure Keyvault secretSpring Boot 应用程序访问 Azure Keyvault 机密
【发布时间】:2023-01-12 00:21:40
【问题描述】:

我正在尝试设置 Spring Boot 应用程序以连接到 Azure keyvault 并获取机密。 我查看了各种代码示例,但无法让其中一个正常工作。

没有与 keyvault 库本身相关的错误,但是 Spring bean 没有使用从 secret 中获取的属性进行初始化。

我使用的是 Spring boot 2.7.3,keyvault starter lib 是 4.2.0。这就是我提到的获取我的属性配置。 https://microsoft.github.io/spring-cloud-azure/4.2.0/reference/html/appendix.html#_azure_key_vault_secrets_properties

application.yml
------------
spring:
  cloud:
    azure:
      keyvault:
        secret:
          enabled: true
          endpoint: https://uri/
          credential:
            client-certificate-path: <path to pem file>
            client-id: <client-id>
          profile:
            tenant-id: <tenant-id>


build.gradle
--------------
implementation 'com.azure.spring:spring-cloud-azure-starter-keyvault-secrets:4.2.0'


Spring bean
-------------

@Configuration
@Getter
@Setter
public class AKVProperties {
 
 
  @Value("${MYAPP-DB-LOGIN-PASSWORD}")
  private String dbPassword1;
  ...

Exception at startup:
----------------------
Error creating bean with name 'AKVProperties': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'MYAPP-DB-LOGIN-PASSWORD' in value "${MYAPP-DB-LOGIN-PASSWORD}"

【问题讨论】:

    标签: spring-boot azure-keyvault


    【解决方案1】:
    • 我认为问题出在application.yaml 文件上。您必须在 property-sources: 下设置您的配置设置。

    • 这是我在 property-sources: 下设置的 application.yaml

    spring:
      cloud:
        azure:
          keyvault:
            secret:
              property-sources:
                - credential:
                    client-id: <CLIENT ID>
                    client-secret: <CLIENT SECRET>
                  endpoint: '<END POINT>'
                  profile:
                    tenant-id: <TENANT ID>
    
    • 在这里我有这样的秘密
    @Value("${secret}")  
    private String secret;
    

    然后我只是控制台记录它们。

    输出:

    密钥库:

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 2020-02-05
      • 1970-01-01
      • 2021-04-18
      • 2015-05-27
      • 1970-01-01
      • 2019-01-09
      • 2021-06-22
      • 1970-01-01
      相关资源
      最近更新 更多