【问题标题】:Authentication of Azure using azure java sdk使用 azure java sdk 对 Azure 进行身份验证
【发布时间】:2020-06-17 09:56:32
【问题描述】:

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

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();

【问题讨论】:

    标签: azure azure-authentication azure-java-sdk


    【解决方案1】:

    如果你想获取访问令牌,你可以试试下面的code

    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中的依赖这样,或者下载jarhere:

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

    更多详情,请参阅sample


    401 错误与您的权限有关。 Get subscriptions 需要范围https://management.azure.com/.default

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2013-03-31
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多