使用Azure Java SDK进行Azure身份验证 [英] Authentication of Azure using azure java sdk

查看:104
本文介绍了使用Azure Java SDK进行Azure身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是检查客户端,租户,秘密密钥是否有效的正确方法吗?那么此代码需要哪些jar,以及在哪里下载?

Is this the correct way of checking client, tenant,secret key are valid? then what are the jars required to this code and where to download it?

String client = "xxxxxxxxxxx";
String tenant = "xxxxxxxxxxx";
String key = "xxxxxxxxxxx";
String subscriptionId = "xxxxxxxxxxx";

ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client, tenant,key, AzureEnvironment.AZURE);
Azure azure = Azure.configure().authenticate(credentials).withDefaultSubscription();
azure.subscriptions().list();

推荐答案

如果要获取访问令牌,可以尝试

If you want to get access token, you could try the code below.

private static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception {

        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                CONFIDENTIAL_CLIENT_ID,
                ClientCredentialFactory.createFromSecret(CONFIDENTIAL_CLIENT_SECRET))
                .authority(TENANT_SPECIFIC_AUTHORITY)
                .build();

        // With client credentials flows the scope is ALWAYS of the shape "resource/.default", as the
        // application permissions need to be set statically (in the portal), and then granted by a tenant administrator
        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                Collections.singleton(GRAPH_DEFAULT_SCOPE))
                .build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();
    }

像这样在pom.xml中的依赖项,或者在此处a>:

The dependency in the pom.xml like this, or dowanload the jar here:

    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>msal4j</artifactId>
        <version>1.2.0</version>
    </dependency>

有关更多详细信息,请参见样本.

For more details, see the sample.

401错误与您的许可有关.获取订阅需要范围 https://management.azure.com/.default.

401 error is related to your permission. Get subscriptions need scope https://management.azure.com/.default.

这篇关于使用Azure Java SDK进行Azure身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆