问题描述

使用Java SDK获取Key Vault Secret机密信息时,需要获取授权。通常是使用AAD的注册应用(Client ID, Tenant ID, Client Secret)来获取 credential 对象。

        SecretClient secretClientidentity = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(new DefaultAzureCredentialBuilder()
.authorityHost(AzureAuthorityHosts.AZURE_CHINA)
.build())
.buildClient();

如果使用 DefaultAzureCredentialBuilder  来创建,则需要把 Client ID, Secret 和 Tenant ID 设置为环境变量。

如果不想设置环境变量,而想直接把三个参数值通过代码传递,是否有示例代码呢?

问题解答

可以的。使用 ClientSecretCredentialBuilder 就可以把Client ID,Secret 和 Tenant ID 参数显示设置。

代码如下:

        // /**
// * Authenticate with client secret.
// */
String clientID="xxxxxxxx-8216-xxxxxxxx-8924-xxxxxxxxxxxxxxxx";
String tenantID="xxxxxxxx-66d7-xxxxxxxx-8f9f-xxxxxxxxxxxxxxxx";
String clientSecret="xxxxxxxx.3ay_aOti..4";
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(clientID)
.clientSecret(clientSecret)
.tenantId(clientSecret)
.authorityHost(AzureAuthorityHosts.AZURE_CHINA)
.build(); SecretClient secretClientidentity = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(clientSecretCredential)
.buildClient();

完整代码

package com.example.demokeyvault;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import com.azure.identity.AzureAuthorityHosts;
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret; @SpringBootApplication
public class DemokeyvaultApplication { public static void main(String[] args) {
SpringApplication.run(DemokeyvaultApplication.class, args); System.out.println("Hello World!");
String keyVaultUri = "https://yourkeyvaultname.vault.azure.cn/"; System.out.printf(" key vault URI = %s \n", keyVaultUri); // String userIdentityID = " - - - - ";
// .managedIdentityClientId(userIdentityID) // /**
// * Authenticate with client secret.
// */
String clientID = "xxxx-xxxx-xxxx-xxxx-xxxx";
String tenantID = "xxxx-xxxx-xxxx-xxxx-xxxx";
String clientSecret = "xxxx.xxxx..4";
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(clientID)
.clientSecret(clientSecret)
.tenantId(clientSecret)
.authorityHost(AzureAuthorityHosts.AZURE_CHINA)
.build(); SecretClient secretClientidentity = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(clientSecretCredential)
.buildClient(); // /**
// * Authenticate with DefaultAzureCredentialBuilder.
// */
// SecretClient secretClientidentity = new SecretClientBuilder()
// .vaultUrl(keyVaultUri)
// .credential(new DefaultAzureCredentialBuilder()
// .authorityHost(AzureAuthorityHosts.AZURE_CHINA)
// .build())
// .buildClient(); String secretName = "testsecret01"; KeyVaultSecret retrievedSecret = secretClientidentity.getSecret(secretName); System.out.println("Your secret's value is '" + retrievedSecret.getValue() + "'."); System.out.println("done.");
} }

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demokeyvault</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demokeyvault</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.2.3</version>
</dependency> <dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.4.19</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

参考资料

Client secret credential : https://learn.microsoft.com/en-us/azure/developer/java/sdk/identity-service-principal-auth#client-secret-credential

适用于 Java 的 Azure Key Vault 机密客户端库 : https://docs.azure.cn/zh-cn/key-vault/secrets/quick-create-java?tabs=azure-cli

【Azure Developer】Java代码访问Key Vault Secret时候的认证问题,使用 DefaultAzureCredentialBuilder 或者 ClientSecretCredentialBuilder的更多相关文章

  1. 【Azure Developer】记录一次使用Java Azure Key Vault Secret示例代码生成的Jar包,单独运行出现 no main manifest attribute, in target/demo-1.0-SNAPSHOT.jar 错误消息

    问题描述 创建一个Java Console程序,用于使用Azure Key Vault Secret.在VS Code中能正常Debug,但是通过mvn clean package打包为jar文件后, ...

  2. Oracle03——游标、异常、存储过程、存储函数、触发器和Java代码访问Oracle对象

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7476717.html 1.游标(光标)Cursor 在写java程序中有集合的概念,那么 ...

  3. 模拟安装redis5.0集群并通过Java代码访问redis集群

    在虚拟机上模拟redis5.0的集群,由于redis的投票机制,一个集群至少需要3个redis节点,如果每个节点设置一主一备,一共需要六台虚拟机来搭建集群,此处,在一台虚拟机上使用6个redis实例来 ...

  4. 在linux安装redis单机和集群后,如何在windows上使用redis客户端或者java代码访问错误的原因很简单,就是没有连接上redis服务,由于redis采用的安全策略,默认会只准许本地访问。需要通过简单配置,完成允许外网访问。

    这几天在学习在linux上搭建服务器的工作,可谓历经艰辛.可喜最后收获也不少. 这次是在linux上搭建redis服务器后从windows上缺无法访问,连接不上. 仔细回忆以前搭建nginx和ftp的 ...

  5. 【Azure Developer】使用 CURL 获取 Key Vault 中 Secrets 中的值

    问题描述 在使用CURL通过REST API获取Azure Key Vaualt的Secrets值,提示Missing Token, 问如何来生成正确的Token呢? # curl 命令 curl - ...

  6. 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)

    关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...

  7. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

  8. 【Azure Developer】使用Java代码启动Azure VM(虚拟机)

    问题描述 在使用Java的启动Azure VM的过程中,遇见了com.azure.core.management.exception.ManagementException: Status code ...

  9. Azure Key Vault (1) 入门

    <Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...

  10. Azure Key Vault (2) 使用Azure Portal创建和查看Azure Key Vault

    <Windows Azure Platform 系列文章目录> 请注意: 文本仅简单介绍如何在Azure Portal创建和创建Key Vault,如果需要结合Application做二次 ...

随机推荐

  1. 【分享代码片段】terraform中,如何从刚刚创建的 deployment 中获得所有容器的名字和 ip

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 不好意思,刚刚才开始用 terraform,或许是更好的办 ...

  2. FaceFusion:探索无限创意,创造独一无二的面孔融合艺术!

    FaceFusion:探索无限创意,创造独一无二的面孔融合艺术! 它使用先进的图像处理技术,允许用户将不同的面部特征融合在一起,创造有趣和令人印象深刻的效果.这个项目的潜在应用包括娱乐.虚拟化妆和艺术 ...

  3. LeetCode刷题日记 2020/03/26

    题干 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...

  4. 苹果M3 Max有两种版本:14+40?还是16+40?

    最近有关苹果M3系列处理器的消息突然多了起来,包括M3.M3 Pro.M3 Max,都将升级为台积电3nm工艺,但规格上比较保守,至少核心数量不会大幅增加. 此前说法称,M3 Max将配备14个CPU ...

  5. 【奶奶看了都会】云服务器部署开源ChatGLM-6B,让你也能拥有自己的ChatGPT

    1.背景 大家好啊,我是小卷. 最近ChatGPT不仅发布了GPT-4,而且解除封印可以联网了.不得不赞叹AI更新迭代的速度真快,都跟不上节奏了.但是大家也注意到了吧,随着ChatGPT的每次更新,O ...

  6. ElasticSearch7.3学习(十)----采用restful风格对索引的增删改查

    1. 为什么需要手动创建索引 直接put数据 PUT index/_doc/1,es会自动生成索引,并建立动态映射dynamic mapping.这样的话很大可能与实际的需求不服,在实际的应用上,我们 ...

  7. Linux-Shell变量的算术运算

    一.算术运算符 1.+.- 求和. a+b   a-b 2. *./.% 求乘积,商,余数    a*b   a/b    a/b 3.** 幂运算,例如 3**3 是求 3 的立方,即 27 4. ...

  8. Pandas练习

    背景介绍 本数据集包括了2015年至2017年我国36个主要一线城市.特区的一些年度数据,包括产值.人口.就业.教育.医疗.经济贸易.房地产投资等方面. 包含文件: 2015年国内主要城市年度数据.c ...

  9. NC15479 最短路

    题目链接 题目 题目描述 企鹅国中有 \(N\) 座城市,编号从 \(1\) 到 \(N\) . 对于任意的两座城市 \(i\) 和 \(j\),企鹅们可以花费 \((i\,\,xor\,\, j)* ...

  10. Redhat7更改网易yum源

    说明 之前写了一篇关于Redhat6更换Yum源的文章,时隔已久很多包都变了,正好最近搭建环境需要用到Redhat7.3所以就再记录一下如何更换为国内最新最常用的yum源. 操作步骤 1.卸载系统自带 ...