Azure Key Vault错误消息“AKV10000:请求缺少承载或PoP令牌,”

polhcujo  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(124)

我在Azure上有Key Vault,我为具有Secret权限的应用程序注册添加了访问策略(获取,列表,设置,删除,恢复,备份,还原)我编写了Java代码:

String keyVaultUri = "https://myKeyVault.azure.net";

    ClientSecretCredential paramClientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId("MyApplicationId")
            .clientSecret("MyApplicationSecretKey")
            .tenantId("MyTenantId")
            .build();

     SecretClient secretClient = new SecretClientBuilder()
        .vaultUrl(keyVaultUri)
        .credential(paramClientSecretCredential)
        .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
        .buildClient();

     KeyVaultSecret storedSecret = secretClient.getSecret("mySecret");

     System.out.println("Secret value: "+storedSecret.getValue());

我正在使用这些Maven依赖项:

  1. azure-security-keyvault-secretss(版本4.6.2)
  2. azure-core(版本1.39.0)
  3. azure-identity(版本1.9.0-beta.1)
    在我开始我的代码后,我得到了这个错误消息Status code 401,“{“error”:{“code”:“Unauthorized”,“message”:“AKV 10000:请求缺少承载令牌或PoP令牌。
    你对如何解决这个问题有什么建议吗?
txu3uszq

txu3uszq1#

我对你的代码做了一些修改,我在输出端得到了我的密钥库的秘密。

代码:

import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;

public class KeyVaultKam {
    public static void main(String[] args) {
        String keyVaultUri = "https://<keyvault-name>.vault.azure.net/";
        String secretName = "<secrete-name>";

        ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                .clientId("<client-ID>")
                .clientSecret("<client-secrete>")
                .tenantId("<tenant-ID>")
                .build();

        SecretClient secretClient = new SecretClientBuilder()
                .vaultUrl(keyVaultUri)
                .credential(credential)
                .buildClient();

        try {
            KeyVaultSecret secret = secretClient.getSecret(secretName);
            System.out.println("Secret value: " + secret.getValue());
        } catch (Exception e) {
            System.out.println("Error retrieving secret: " + e.getMessage());
        }
    }
}

pom.xml:

我将下面的dependencies添加到pom.xml文件中

<dependencies>
     
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-security-keyvault-secrets</artifactId>
            <version>4.6.2</version>
        </dependency>
        
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-core</artifactId>
            <version>1.39.0</version>
        </dependency>
        
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.4.0</version>
        </dependency>
        
</dependencies>

我为客户端应用创建了访问策略,如下所示:

输出:
运行成功,得到我的secret的输出如下:

相关问题