首页 > 编程语言 >【Azure Developer】Java代码访问Key Vault Secret时候的认证问题,使用 DefaultAzureCredentialBuilder 或者 ClientSecretCre

【Azure Developer】Java代码访问Key Vault Secret时候的认证问题,使用 DefaultAzureCredentialBuilder 或者 ClientSecretCre

时间:2022-09-28 20:48:39浏览次数:87  
标签:xxxx String DefaultAzureCredentialBuilder boot Secret import azure Java com

问题描述

使用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

 

标签:xxxx,String,DefaultAzureCredentialBuilder,boot,Secret,import,azure,Java,com
From: https://www.cnblogs.com/lulight/p/16739485.html

相关文章

  • 计算机类毕业设计选题推荐 springboot+vue宠物医院管理系统 java宠物预约挂号系统 微
    ......
  • JavaDoc
    packagebase;publicclassDoc{Stringname;/****@paramname;*@return;*@throwsException;*///由/**回车打出......
  • 【Java基础】自定义异常类
    1.自定义异常类(1)继承现有异常类:比如RuntimeException、Exception。(2)提供全局常量:serialVersionUID。(3)提供重载构造器。publicclassValueExceptionextendsException......
  • 【Java基础】抛出异常throws
    1.throws声明抛出异常一个方法中可能出现异常,那么可以使用throws抛出异常,交给这个方法的调用者处理(也不处理就继续向上抛,直到main方法)。publicvoidsay()throwsCl......
  • 【Java基础】异常处理机制:try-catch-finally
    1.异常对象生成(抛出异常)Java程序的执行过程中如果出现异常,就会生成一个异常类的对象,该异常对象将会被提交给Java运行时系统。虚拟机自动生成异常对象:虚拟机检测到程序......
  • JAVA Spring学习笔记 ------Bean
    Bean容器为什么要使用Bean容器因为希望减少new的操作,这样当我们的类名发生改变时,影响会降到最小Bean相当于一个管家原来需要我们自己new的对象现在统统通过Bean来创建......
  • JAVA操作Excel (POI)
    packagecom.quantum.utils;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.*;importorg.apache.poi.xssf.usermodel.XSSFW......
  • JAVA Spring学习笔记------注解开发
    注解开发可以在对应的类中配置Bean具体方法如下:利用@Component来声明当前类为Bean@Component("userDaoimpl")//Reposity和component效果一样//spring对于不同的层......
  • JAVA Spring学习笔记------Spring整合Mybatis
    Spring整合Mybatis首先看一下项目目录   因为以前所有的知识点都在一个模块里,而且目录比较混乱所以对模块重构了一下首先我们要在pom.xml文件中声明所需要的依赖......
  • java异常
    西二旗是一个恐怖的地铁站,光排队得排很久。只要写代码就存在bug,唯一一个不出bug得方法就是不写代码。异常:在程序的运行过程中,出现的不正常情况叫做异常.注意:1.相同的代......