spring-boot - 访问KeyVault时出现SslHandshakeTimeoutException(SpringBoot 2.7)

标签 spring-boot azure azure-keyvault

我正在尝试学习如何从 Azure KeyVault 获取 secret 。所以我用我的帐户设置了一个 KeyVault。我想我做对了。但是当我尝试从 SpringBoot 应用程序连接它时,出现以下错误:

2023-06-14 19:45:27.239 INFO 7360 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-06-14 19:45:27.255 INFO 7360 --- [  restartedMain] c.example.demo.KeyvaultDemoApplication   : Started KeyvaultDemoApplication in 2.898 seconds (JVM running for 3.904)
2023-06-14 19:45:38.765 ERROR 7360 --- [  restartedMain] c.a.c.http.netty.NettyAsyncHttpClient    : io.netty.handler.ssl.SslHandshakeTimeoutException: handshake timed out after 10000ms
2023-06-14 19:45:38.775 WARN 7360 --- [ctor-http-nio-1] r.netty.http.client.HttpClientConnect    : [379c4b95, L:/MY_IP:57095 - R:my_key_vault.vault.azure.net/SOME_IP:443] The connection observed an error

io.netty.handler.ssl.SslHandshakeTimeoutException: handshake timed out after 10000ms
    at io.netty.handler.ssl.SslHandler$7.run(SslHandler.java:2125) ~[netty-handler-4.1.92.Final.jar:4.1.92.Final]
    at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
    at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:153) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]

我已经在我的application.properties中设置了这个:

spring.cloud.azure.keyvault.secret.endpoint=https://my_key_vault.vault.azure.net/

我的 SpringBoot 主类如下所示:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.azure.security.keyvault.secrets.SecretClient;

@SpringBootApplication
public class KeyvaultDemoApplication implements CommandLineRunner {


    // Spring Cloud Azure will automatically inject SecretClient in your ApplicationContext.
    @Autowired
    private final SecretClient secretClient;


    public KeyvaultDemoApplication(SecretClient secretClient) {
        this.secretClient = secretClient;
    }


    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("h2url: " + secretClient.getSecret("dbPassword").getValue());
    }


    public static void main(String[] args) {
        SpringApplication.run(KeyvaultDemoApplication.class, args);
    }
}

我写了一个配置类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;


@Configuration
public class SecretClientConfiguration {

    @Bean
    public SecretClient createSecretClient() {
        return new SecretClientBuilder().vaultUrl("https://my_key_vault.vault.azure.net/
").credential(new DefaultAzureCredentialBuilder().build()).buildClient();
        }
    }

其中大部分来自 Azure 教程。我很确定,这一定是我设置 KeyVault 的方式。我不是 Azure 中访问控制和其他项目的专家。

任何指示都会有帮助。谢谢。

最佳答案

我按照替代方法从 Spring Boot 应用程序中的 azure key Vault 获取 secret :

请参阅我的 github存储库以获取从 Azure key 保管库获取 secret 的完整代码:

  1. 运行命令 az account list在 Azure Cloud Shell 中获取您帐户的订阅列表。

  2. 运行az account set -s <your_subscription_id>选择并使用特定订阅:

  3. 使用以下命令创建服务主体: az ad sp create-for-rbac --name myapp --role Contributor --scopes /subscriptions/SubscriptionID

  4. 在 Active Directory 中打开您的服务主体应用程序并记下 your_Client_ID 的详细信息, your_Client_Secret_Value your_Tenant_ID

  5. 按照以下步骤在 sprint boot 应用程序中访问 Azure key 保管库:

  • 在门户中创建资源组。
  • 在您的资源组中创建新的 Key Vault。
  • 在 Azure key 保管库中创建 key 并存储您的 key 。
  • 选择“访问策略” => 点击创建并选择所需的权限,例如“获取、列出、恢复、设置、删除、备份、恢复” secret 权限

enter image description here

  • 点击“下一步”并选择您的服务主体应用和“创建”。

enter image description here

  • 打开您的 Spring Boot 应用程序=> resources文件夹=>打开application.properties ,粘贴 your_Client_ID 的值, Client_Secret_Value , Tenant_IDKey Vault URL如下图:
  • enter image description here

    spring.cloud.azure.keyvault.secret.property-sources[0].credential.client-id=<Client_ID>  
    spring.cloud.azure.keyvault.secret.property-sources[0].credential.client-secret=<Client_Secret_value>  
    spring.cloud.azure.keyvault.secret.property-sources[0].endpoint=<your_KeyVault_URL>  
    spring.cloud.azure.keyvault.secret.property-sources[0].profile.tenant-id=<Tenant_ID>
    

    输出:

    enter image description here

    关于spring-boot - 访问KeyVault时出现SslHandshakeTimeoutException(SpringBoot 2.7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76474626/

    相关文章:

    python - 在 Azure 中存储服务主体凭据以使用 Python 对 Key Vault 进行身份验证

    azure - Azure Key Vault 的 MSI 身份验证无法在本地使用 VS 2017 和 IISExpress

    java - spring-boot:mockito忽略测试标准并返回通过的测试

    java - Spring Boot 安全 Rest API

    asp.net - 如何在 Windows Azure 上使用多个实例的 asp.net 缓存

    c# - 如何在不将客户端 key 存储在 appsettings.json 中的情况下获取 Azure Key Vault key ?

    java - Spring Boot 2.0 中的 EmbeddedServletContainerAutoConfiguration 和 ServerPropertiesAutoConfiguration

    spring - 嵌入式tomcat Spring boot中的ssl设置

    azure - 术语 'msbot' 未被识别为 cmdlet、函数、脚本文件或可操作程序的名称

    azure - 如何从 API 管理将查询参数发送到逻辑应用