简体   繁体   English

是否可以将 DeviceCode 身份验证流程与 Azure Java SDK 一起使用?

[英]Is it possible to use the DeviceCode authentication Flow with Azure Java SDK?

I successfully generate an IAuthenticationResult using the azure msal4j library - I am presented with a device code, and when that code is typed into a browser, it shows the correct scopes / permissions, and now I'd like to take this authentication result and pass it into the Azure-SDK authentication similar to:我使用 azure msal4j库成功生成了IAuthenticationResult - 我看到了一个设备代码,当将该代码输入浏览器时,它显示了正确的范围/权限,现在我想获取此身份验证结果并通过它进入 Azure-SDK 身份验证,类似于:

    val result = DeviceCodeFlow.acquireTokenDeviceCode()


    val a: Azure = Azure.configure()
        .withLogLevel(LogLevel.BODY_AND_HEADERS)
        .authenticate(AzureCliCredentials.create(result))
        .withDefaultSubscription()

Does anyone know where to look / or any samples which do this?有谁知道在哪里看/或任何这样做的样本?

If you want to use msal4j library to get access token, then use the token to manage Azure resource with Azure management SDK, please refer to the following code如果要使用msal4j库获取访问令牌,则使用令牌管理 Azure 资源与 Azure 管理 SDK,请参考以下代码

public class App {
    public static void main(String[] args) throws Exception {
        String subscriptionId = ""; // the subscription id
        String domain="";// Azure AD tenant domain 
        DeviceCodeTokenCredentials tokencred = new DeviceCodeTokenCredentials(AzureEnvironment.AZURE,domain);
         Azure azure =Azure.configure()
                           .withLogLevel(LogLevel.BASIC)
                           .authenticate(tokencred)
                           .withSubscription(subscriptionId);
                                  
         for(AppServicePlan plan : azure.appServices().appServicePlans().list()) {
                  
                  System.out.println(plan.name());
                  
                  }
    }  
}

// define a class to extend AzureTokenCredentials
 class DeviceCodeTokenCredentials extends AzureTokenCredentials{

    public DeviceCodeTokenCredentials(AzureEnvironment environment, String domain) {
        super(environment, domain);
    }

    @Override
    public String getToken(String resource) throws IOException {
        // use msal4j to get access token 
        String clientId="d8aa570a-68b3-4283-adbe-a1ad3c1dfd8d";// you Azure AD application app id
        String AUTHORITY = "https://login.microsoftonline.com/common/";
        Set<String> SCOPE = Collections.singleton("https://management.azure.com/user_impersonation");
        PublicClientApplication pca = PublicClientApplication.builder(clientId)
                .authority(AUTHORITY)
                .build();

        Consumer<DeviceCode> deviceCodeConsumer = (DeviceCode deviceCode) ->
        System.out.println(deviceCode.message());

      DeviceCodeFlowParameters parameters =
        DeviceCodeFlowParameters
                .builder(SCOPE, deviceCodeConsumer)
                .build();
      IAuthenticationResult result = pca.acquireToken(parameters).join();       
      return result.accessToken();
    } 
 }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 身份验证 Azure Java SDK - Authentication Azure Java SDK AWS Lambda:可以使用Oracle Java SDK吗? - AWS Lambda: it is possible to use Oracle Java SDK? 如何为 azure java sdk 使用“com.azure”包 - How to use "com.azure" package for azure java sdk 如何配置 AWS 用户认知身份验证流程以在 Java sdk 后端生成身份令牌、访问令牌? - How to configure AWS user cognito authentication flow for generating identity token,access token in Java sdk backend? 适用于通过Cognito用户池为Oauth客户端凭据流身份验证提供服务的AWS Java SDK吗? - AWS Java SDK for service to service Oauth client credentential flow authentication with Cognito user pool? 桌面Flickrj Java身份验证流程 - Desktop Flickrj Java Authentication Flow IBM Watson API Java SDK使用Watson令牌身份验证失败 - IBM Watson APIs Java SDK use Watson token authentication fail Java中的窗口天蓝色身份验证 - window azure authentication in java 如何在Java中使用身份验证方法在Azure VM上连接WASB - How to use authentication methods in java to connect WASB on Azure VM 通过具有身份验证的代理使用azure-notificationhubs-java-backend - Use azure-notificationhubs-java-backend via proxy with authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM