我有基于Java的高容量多线程应用程序,它需要在使用“Azure ADAL AcquireToken”检索的标头中使用授权令牌调用在Microsoft云上运行的基于REST的端点。我正在使用“AzureAD / azure-activedirectory-library-for-java”(下面的代码示例)。我遇到的问题是 -
我的代码如下。当我从main方法中的循环调用acquireToken方法时,我在10个调用中大多有3种不同类型的令牌,并且所有3种不同的令牌似乎都有效,但不确定它是否应该在多线程应用程序中调用它。
package com.mycompany.msft.auth;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
public class ApplicationAuthExample {
private final static String AUTHORIZATION_ENDPOINT = "https://login.microsoftonline.com/";
private final static String ARM_ENDPOINT = "https://myendpoint";
private static String credential = "my credential";
private static String clientId = "my client id";
private static String tenantId = "my tenant id";
private static String url = AUTHORIZATION_ENDPOINT + tenantId ;
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
public AuthenticationResult getAuthToken() {
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(url, false, service);
Future<AuthenticationResult> future = null;
ClientCredential cred = new ClientCredential(clientId, credential);
future = context.acquireToken(ARM_ENDPOINT, cred, null);
result = future.get();
} catch (Exception ex) {
System.out.println("Exception occurred:");
ex.printStackTrace();
System.exit(1);
} finally {
service.shutdown();
}
return result;
}
public static void main(String[] args) throws Exception {
ApplicationAuthExample auth = new ApplicationAuthExample();
for (int i =0 ; i< 10 ; i++) {
AuthenticationResult result = auth.getAuthToken();
// use adal to Authenticate
System.out.println (i+ " Authorization" + "Bearer " + result.getAccessToken());
System.out.println (i + " getExpiresOn" + result.getExpiresOn());
//This token comes different in different calls. Which one should I use and which one not.
System.out.println (i+ " getExpiresOn" + result.getRefreshToken());
System.out.println (i+" getExpiresOn" + result.getUserInfo());
}
}
}
答案 0 :(得分:0)
根据我的经验,我认为问题的关键是令牌的到期时间。您可以在到期后使用令牌作为您的愿望。您可以参考https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-tokens/的Issued At
部分,了解该令牌声称时间包括Expriation Time
,adal4j
&amp; “不是之前。”
因此,您需要使用Content-Type: application/json, application/x-www-form-urlencoded
获取令牌,并在前一个令牌到期时请求刷新令牌。
使用AD FS 2.0进行基于声明的身份验证部署的默认安全令牌的生存期为60分钟。
如果要增加Azure AD的令牌过期时间,可以尝试引用文档https://technet.microsoft.com/en-us/library/gg188586.aspx来配置信赖方令牌生存期。