【问题标题】:Azure Active Directory - Setting Token Expiry in javaAzure Active Directory - 在 java 中设置令牌到期
【发布时间】:2019-07-09 16:55:56
【问题描述】:

我是 azure 的新手,我正在尝试在 java 中为我的访问令牌设置到期时间,但我找不到任何示例来设置它。请帮助如何设置到期时间,从 60 分钟到 1 天。

我想将 "expires_in": "3600" 增加到至少 8 小时或更长。

在代码中,我无法获得任何方法,如 set param 或 set headers。请帮助我怎么做。

这是参数的链接:

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-configurable-token-lifetimes#cmdlet-reference

谢谢。

AuthenticationContext context = null;
        AuthenticationResult result = null;
        ExecutorService service = null;
        try {

              String refreshToken = request.getParameter("refreshToken");
              String currentUri   = request.getRequestURL().toString();  

            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(authority + tenant + "/", true,
                    service);
            Future<AuthenticationResult> future = context
                    .acquireTokenByRefreshToken(refreshToken,
                            new ClientCredential(clientId, clientSecret), null,
                            null);

            result = future.get();

  //////////////////////////////////////////////////////////
  // token values I'm getting

{
 "access_token": "<requested-access-token>",
 "token_type": "<token-type-value>",
 "expires_in": "3600",

"expires_on": "<access-token-expiration-date-time>",
 "resource": "<app-id-uri>",
 "refresh_token": "<oauth2-refresh-token>",
 "scope": "user_impersonation",

 "id_token": "<unsigned-JSON-web-token>"
}

【问题讨论】:

    标签: azure active-directory azure-active-directory skype-for-business ucwa


    【解决方案1】:

    您将需要使用 Powershell 脚本来创建策略。这是供您参考的脚本。

    Install-Module -Name AzureADPreview -Force
    
    Connect-AzureAD -confirm
    
    $policy=New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"08:00:00"}}') -DisplayName "tonytestpolicy" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"
    

    如果你必须在你的java代码中这样做,你可以使用graph api。(powershell脚本也调用这个api)。这是sample

    POST https://graph.microsoft.com/beta/policies
    Content-Type: application/json
    
    {
      "displayName":"CustomTokenLifetimePolicy",
      "definition":["{\"TokenLifetimePolicy\":{\"Version\":1,\"AccessTokenLifetime\":\"8:00:00\"}}"],
      "type":"TokenLifetimePolicy"
    }
    

    【讨论】:

    • 我按照您的发布请求生成了访问令牌,我收到 { "error": { "code": "Authorization_RequestDenied", "message": "Insufficient rights to complete the operation.", "innerError": { "request-id": "b29a2d28-41a3-4db0-81fb-394e66df74dc", "date": "2019-07-10T05:36:01" } } } 使用不记名令牌更新时,尝试更新这个..你能指导我吗..谢谢你。
    • @curious_one 您是否授予了 Directory.AccessAsUser.All 权限?
    • @curious_one 我发现您的代码正在使用不支持此 api 的客户端凭据流。在此处查看调用 api 所需的权限。 docs.microsoft.com/en-us/graph/api/…
    • @curious_one 你必须在你的代码中这样做吗?您可以使用 powershell 或图形资源管理器来执行此操作。这是图形资源管理器developer.microsoft.com/en-us/graph/graph-explorer
    • 我很抱歉没有回复,我现在得到了一些不同的任务,所以我没有检查这个。所以我会在一段时间后检查。如果它有效,我一定会毫不拖延地将其标记为答案。谢谢你的宝贵帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多