java - 如何在java中获取客户端 key 到期日期

标签 java azure azure-active-directory

我在 Azure AD 租户中注册了应用程序,这些应用程序具有 clientid 和 key 。 我需要获取 azure 应用程序凭据的到期日期。我在我的应用程序中使用azure sdk for java。 我们如何使用 java 获取客户端 secret 的到期日期?

我用谷歌搜索过,但没有找到任何有用的链接。谁能帮我解决这个问题。

最佳答案

如果您想在 Java 应用程序中获取 secret 到期日期,您可以调用 Microsoft Graph API获取应用程序。然后应用程序的属性passwordCredentials有信息。例如

使用 Azure 门户注册新应用程序

  1. 使用工作或学校帐户登录 Azure 门户,或者 个人 Microsoft 帐户。

  2. 如果您的帐户允许您访问多个租户,请在右上角选择您的帐户,然后将您的门户 session 设置为所需的 Azure AD 租户。

  3. 在左侧导航 Pane 中,选择 Azure Active Directory 服务,然后选择应用注册 > 新注册。

配置应用程序所需的 Microsoft Graph 权限 enter image description here

代码

  //install ADAL4J get accesss token
    String clientId = "your application id";
    String appKey = "your client secret";
    String tenantId = "your tenant id";
    String authority =String.format("https://login.microsoftonline.com/",getTenantContextId())
    String resourceUrl = "https://graph.microsoft.com"
    ExecutorService service =  Executors.newFixedThreadPool(1);
    AuthenticationContext context = ew AuthenticationContext(authority, false, service); 
    ClientCredential clientCred = new ClientCredential(
                clientId, appKey);
    Future<AuthenticationResult> future = context.acquireToken(resourceUrl, clientCred, null);
     AuthenticationResult result = future.get();
    
    //Call Microsoft graph api
   String stringUrl ="https://graph.microsoft.com/beta/applications?$filter=appId eq '{ApplicationId}'";
        URL url = new URL(stringUrl.replaceAll(" ","%20"));

         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Bearer " + result.getAccessToken());
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Content-Type", "application/json");

        int httpResponseCode = conn.getResponseCode();
        if (httpResponseCode == 200 ) {
            BufferedReader in = null;
             StringBuilder response;

                in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String inputLine;
                response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }

                in.close();
            JSONObject jb = new JSONObject(response.toString());

  

更多详情请引用document

更新

请使用以下代码获取访问 token

String authority = "https://login.microsoftonline.com/" + tenant;
        ExecutorService service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = new AuthenticationContext(authority, true, service);
        ClientCredential cred = new ClientCredential(clientId, clientSecret);
        String resourceId ="https://graph.microsoft.com";
        Future<AuthenticationResult> future = context.acquireToken(resourceId, cred, null);
        AuthenticationResult result = future.get();
        String accesstoken = result.getAccessToken();

enter image description here

关于java - 如何在java中获取客户端 key 到期日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57556944/

相关文章:

azure-active-directory - 如何在 Microsoft.Identity.Web 中定义 SignedOut 页面?

java - 这段代码是某种命令模式吗?

java - Z3:检查模型是否唯一

java - 访问 for 循环内创建的字符串变量

java - Eclipselink 扩展 JOIN 子句

.NET 6 Linux 多容器应用程序无法在 Azure 应用服务中建立容器内部通信

azure - 如何配置 Azure AD 以启用刷新 token

azure - CosmosDB 是纯粹的键值数据库吗?

azure - 如何在代码中通过 ftp 将文件从网站复制到 azure?

Azure Devops 组织代码部署不显示组织