Azure Active Directory - 在 Java 中设置令牌到期 [英] Azure Active Directory - Setting Token Expiry in java

查看:28
本文介绍了Azure Active Directory - 在 Java 中设置令牌到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to azure and I'm trying to set expiry time for my access token, in java, but im not able to find any examples to do how to set it. Please help how to set the expiry time, from 60 minutes to 1 day.

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

I want to increase the "expires_in": "3600", to atleast 8 hours or more than it.

在代码中,我无法获得任何方法,例如 set param 或 set headers.请帮我我怎么能.

In the code, here I'm not able to get any methods like set param or set headers. Please help me how i can .

这里是参数的链接:

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>"
}

推荐答案

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

You will need to use Powershell script to create the policy. Here is the script for your reference.

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).这是示例.

If you must do this in your java code, you can use graph api.(The powershell script also calls this api). Here is the sample.

POST https://graph.microsoft.com/beta/policies
Content-Type: application/json

{
  "displayName":"CustomTokenLifetimePolicy",
  "definition":["{\"TokenLifetimePolicy\":{\"Version\":1,\"AccessTokenLifetime\":\"8:00:00\"}}"],
  "type":"TokenLifetimePolicy"
}

这篇关于Azure Active Directory - 在 Java 中设置令牌到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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