【Azure Developer】Java代码访问Key Vault Secret时候的认证问题,使用 DefaultAzureCredentialBuilder 或者 ClientSecretCredentialBuilder
问题描述
使用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的更多相关文章
- 【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文件后, ...
- Oracle03——游标、异常、存储过程、存储函数、触发器和Java代码访问Oracle对象
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7476717.html 1.游标(光标)Cursor 在写java程序中有集合的概念,那么 ...
- 模拟安装redis5.0集群并通过Java代码访问redis集群
在虚拟机上模拟redis5.0的集群,由于redis的投票机制,一个集群至少需要3个redis节点,如果每个节点设置一主一备,一共需要六台虚拟机来搭建集群,此处,在一台虚拟机上使用6个redis实例来 ...
- 在linux安装redis单机和集群后,如何在windows上使用redis客户端或者java代码访问错误的原因很简单,就是没有连接上redis服务,由于redis采用的安全策略,默认会只准许本地访问。需要通过简单配置,完成允许外网访问。
这几天在学习在linux上搭建服务器的工作,可谓历经艰辛.可喜最后收获也不少. 这次是在linux上搭建redis服务器后从windows上缺无法访问,连接不上. 仔细回忆以前搭建nginx和ftp的 ...
- 【Azure Developer】使用 CURL 获取 Key Vault 中 Secrets 中的值
问题描述 在使用CURL通过REST API获取Azure Key Vaualt的Secrets值,提示Missing Token, 问如何来生成正确的Token呢? # curl 命令 curl - ...
- 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...
- 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...
- 【Azure Developer】使用Java代码启动Azure VM(虚拟机)
问题描述 在使用Java的启动Azure VM的过程中,遇见了com.azure.core.management.exception.ManagementException: Status code ...
- Azure Key Vault (1) 入门
<Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...
- Azure Key Vault (2) 使用Azure Portal创建和查看Azure Key Vault
<Windows Azure Platform 系列文章目录> 请注意: 文本仅简单介绍如何在Azure Portal创建和创建Key Vault,如果需要结合Application做二次 ...
随机推荐
- Cosmic云星瀚的简单学习-测试用户创建
摘要 上一个学习文档里面总结了: 修改domain的url之后就可以重启服务然后登录了. 今天中午创建了一个业务用户,发现还挺麻烦的 因为可能短信服务有问题, 所以我这边需要有改数据库表的需求. 这里 ...
- Find 查找并且展示最近24小时内创建的文件信息
1. 命令为: find /gscloud/tools/patchinstall/patchfiles/ -maxdepth 1 -mtime 1 |cut -c40- >/deploy/pat ...
- 【转贴】libcrypto.so.10丢失导致sshd无法运行解决方案
http://www.cnblogs.com/billy98/p/4226023.html libcrypto.so.10丢失导致sshd无法运行解决方案 服务器由于掉电开启系统后无法远程ssh,重启 ...
- 我们开源了一个 Ant Design 的单元测试工具库
我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:佳岚 欢迎大家点一个小小的 Star ant-design ...
- 说透IO多路复用模型
作者:京东零售 石朝阳 在说IO多路复用模型之前,我们先来大致了解下Linux文件系统.在Linux系统中,不论是你的鼠标,键盘,还是打印机,甚至于连接到本机的socket client端,都是以文件 ...
- VOP 消息仓库演进之路|如何设计一个亿级企业消息平台
作者:京东零售 李孟冬 VOP作为京东企业业务对外的API对接采购供应链解决方案平台,一直致力于从企业采购数字化领域出发,发挥京东数智化供应链能力,通过产业链上下游耦合与链接,有效助力企业客户的成本优 ...
- 【笔记】VictoriaMetrics中,对大量的pull模式的targets进行分片
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 配置的方法请看这里:https://docs.victor ...
- Go 泛型发展史与基本介绍
Go 泛型发展史与基本介绍 Go 1.18版本增加了对泛型的支持,泛型也是自 Go 语言开源以来所做的最大改变. 目录 Go 泛型发展史与基本介绍 一.为什么要加入泛型? 二.什么是泛型 三.泛型的来 ...
- Python Coroutine 池化实现
Python Coroutine 池化实现 池化介绍 在当今计算机科学和软件工程的领域中,池化技术如线程池.连接池和对象池等已经成为优化资源利用率和提高软件性能的重要工具.然而,在 Python 的协 ...
- 7.4 Windows驱动开发:内核运用LoadImage屏蔽驱动
在笔者上一篇文章<内核监视LoadImage映像回调>中LyShark简单介绍了如何通过PsSetLoadImageNotifyRoutine函数注册回调来监视驱动模块的加载,注意我这里用 ...