基于 Azure Key Vault 证书的身份验证

时间:2021-04-21 20:21:07

标签: azure azure-keyvault

我想连接以使用 Azure AD 客户端证书获取 Key Vault 机密

例子 https://github.com/Azure-Samples/key-vault-java-certificate-authentication 不适合我。

场景步骤

1.在Azure AD中注册应用,添加API/权限名称-Azure Key Vault

选定的 user_impersonation。拥有对 Key Vault 服务的完全访问权限

2.创建证书-pfx文件

  1. 示例中的 Java 代码 - 指定客户端 ID(已注册的应用程序客户端 ID)、pfx 文件密码、pfx 文件位置、密钥库 URL

问题:Azure 如何知道证书?从未使用过 pfx 文件。我需要上传证书吗(上面写着公钥 .cer/pem/crt)

请问我遗漏了哪一步,因为我认为没有进行身份验证?

<块引用>

java.lang.ClassCastException: 类 java.lang.String 不能转换为 class java.util.List(java.lang.String 和 java.util.List 在 模块 java.base of loader 'bootstrap') 在 com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils.getFirstValue(MultivaluedMapUtils.java:70) 在 com.nimbusds.oauth2.sdk.auth.JWTAuthentication.ensureClientAssertionType(JWTAuthentication.java:246)

更正: 我的应用程序不在 Azure VM 中。它是内部部署的

Java 代码:

我用下面的代码更新了 github 示例代码,但在调用acquireAccessToken 时错误相同

AzureAdTokenCredentials credentials = new AzureAdTokenCredentials(
                                        tenant,
                                        AsymmetricKeyCredential.create(clientId, privateKey, certificateKey.getCertificate()),
                                        AzureEnvironments.AZURE_CLOUD_ENVIRONMENT);
                             
TokenProvider provider = new AzureAdTokenProvider(credentials, executorService);

String newToken =  provider.acquireAccessToken().getAccessToken();

                         
<块引用>

java.lang.ClassCastException: 类 java.lang.String 不能转换为 class java.util.List(java.lang.String 和 java.util.List 在 模块 java.base of loader 'bootstrap') 在 com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils.getFirstValue(MultivaluedMapUtils.java:70) 在 com.nimbusds.oauth2.sdk.auth.JWTAuthentication.ensureClientAssertionType(JWTAuthentication.java:246) 在 com.nimbusds.oauth2.sdk.auth.PrivateKeyJWT.parse(PrivateKeyJWT.java:277) 在 com.microsoft.aad.adal4j.AuthenticationContext.createClientAuthFromClientAssertion(AuthenticationContext.java:903)

更多更新:

我在 https://github.com/microsoft/azure-spring-boot/issues/457 中看到上述错误 因此更新了我的一些依赖项并跨过了那座桥。

新错误:

enter image description here

enter image description here 所以我看到它是未经授权的?以及太多的跟进请求。 我该如何解决这个问题?我应该使用 Handling Authentication in Okhttp 吗?这只是为了避免错误,还是保证认证

2 个答案:

答案 0 :(得分:1)

关于问题,请参考以下步骤

  1. 使用 openssl 将 pfx 转换为 cer
openssl pkcs12 -in <> -out <> -nodes
  1. 将证书上传到 Azure AD 应用程序 enter image description here

  2. 为 Azure Key Vault 中的应用程序配置访问策略 enter image description here

  3. 运行示例。

答案 1 :(得分:1)

该死。我认为 AzureAdTokenCredentials 会有所帮助。相反,这有副作用。 在 postman 中看到错误后,我回滚了我的代码更新

<块引用>

"message": "AKV10022:无效的受众。预期 https://vault.azure.net,找到:https://rest.media.azure.net。”

AsymmetricKeyCredential asymmetricKeyCredential = AsymmetricKeyCredential.create(clientId,
                                privateKey, certificateKey.getCertificate());

AuthenticationResult result = context.acquireToken(resource, 
                                asymmetricKeyCredential, null).get();
String newToken = result.getAccessToken();

所以这段代码肯定是设置作用域的,可以在token中看到

<块引用>

"aud": "https://vault.azure.net",

我关注了这篇文章 - https://goodworkaround.com/2020/07/07/authenticating-to-azure-ad-as-an-application-using-certificate-based-client-credential-grant/

如果有人对 openssl 命令等不满意,请在 Key Vault 中创建/生成证书,从那里下载 cer / crt 并导入到 Azure AD 注册应用程序