【问题标题】:MSAL JAVA WEB API for DNS and records用于 DNS 和记录的 MSAL JAVA WEB API
【发布时间】:2020-04-09 09:44:51
【问题描述】:

我们如何使用 Azure Web 服务 API 和不基于 ADAL 的最新“MSAL”库在 azure 服务器上创建区域 dns 和记录?但是 dns 库支持 https://github.com/Azure-Samples/dns-java-host-and-manage-your-domains 没有提到使用 MSAL 访问令牌的任何方式。例如

    ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client, tenant, key, AzureEnvironment.AZURE);
azure = Azure.authenticate(credentials).withSubscription(subscriptionId);
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName)
        .withRegion(Region.US_EAST2)
        .create();

System.out.println("Creating root DNS zone " + customDomainName + "...");
DnsZone rootDnsZone = azure.dnsZones().define(customDomainName)
        .withExistingResourceGroup(resourceGroup)
        .create();

但它使用的是密钥而不是 msal 提供的访问令牌。这可以通过 azure 在内部使用 ADAL 的旧方式实现。

【问题讨论】:

  • 您可以使用msal获取访问令牌,然后使用令牌直接调用Azure rest API(docs.microsoft.com/en-us/rest/api/dns)来管理dns
  • 您还有其他顾虑吗?如果您没有其他顾虑,您能接受吗?它可能会帮助更多有类似问题的人。

标签: java azure dns msal


【解决方案1】:

如果要使用Azure java管理SDK通过AD访问令牌管理Azure DNS,请参考以下代码

一个。创建服务主体(我使用 Azure CLI 执行此操作)

az login
az account set --subscription "<your subscription id>"
# the sp will have Azure Contributor role
az ad sp create-for-rbac -n "readMetric" 

  1. 代码
 public void test() throws MalformedURLException, ExecutionException, InterruptedException {



        AzureTokenCredentials tokenCredentials = new AzureTokenCredentials(AzureEnvironment.AZURE,ADProperty.tenantId) {
            @Override
            public String getToken(String resource) throws IOException {
                String token =null;
                // use msal to get Azure AD access token
                ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                        ADProperty.clientId,  // sp appid
                        ClientCredentialFactory.createFromSecret(ADProperty.clientKey)) // sp password
                        .authority(ADProperty.authority) // "https://login.microsoftonline.com/" + sp tenant id
                        .build();
                ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                        Collections.singleton("https://management.azure.com/.default"))
                        .build();
                CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
                try {
                    token =future.get().accessToken();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                return  token;
            }
        };


        Azure azure = Azure.authenticate(tokenCredentials)
                .withSubscription(ADProperty.subscriptionId); // sp subscription id
        DnsZone rootDnsZone = azure.dnsZones().define("mydevchat.com")
                .withExistingResourceGroup("jimtest")
                .create();
        System.out.println("create DNSZone " + rootDnsZone.name() + " successfully");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2018-08-20
    • 2021-10-24
    相关资源
    最近更新 更多