2016-08-29 154 views
0

我正在使用adal4j v1.1.2基於客戶端證書獲取令牌。adal4j:基於證書的身份驗證錯誤:無效的JWT令牌

片段簡稱:Why does AcquireToken with ClientCredential fail with invalid_client (ACS50012)?

String AAD_HOST_NAME = "login.windows.net"; 
String AAD_TENANT_ID = "XXX"; 
String AAD_TENANT_ENDPOINT = "https://" + AAD_HOST_NAME + "/" + AAD_TENANT_ID + "/"; 
String AAD_CLIENT_ID = "XXX"; 
String AAD_RESOURCE_ID = "https://vault.azure.net"; 
String AAD_CERTIFICATE_PATH = "/XXX.pfx"; 
String AAD_CERTIFICATE_PASSWORD = "XXX"; 
String AAD_CLIENT_SECRET = "XXX"; 

ExecutorService service = ExecutorServiceHelper.createThreadPool(1, "azureHSMClientExecutorService-"); 

KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE"); 
keystore.load(new FileInputStream(AAD_CERTIFICATE_PATH),AAD_CERTIFICATE_PASSWORD.toCharArray()); 
String alias = keystore.aliases().nextElement(); 
PrivateKey key = (PrivateKey) keystore.getKey(alias, AAD_CERTIFICATE_PASSWORD.toCharArray()); 
X509Certificate cert = (X509Certificate) keystore.getCertificate(alias); 
AsymmetricKeyCredential asymmetricKeyCredential = AsymmetricKeyCredential.create(AAD_CLIENT_ID,key, cert); 
AuthenticationContext ctx = new AuthenticationContext(AAD_TENANT_ENDPOINT, false, service); 
Future<AuthenticationResult> result = ctx.acquireToken(AAD_RESOURCE_ID, asymmetricKeyCredential, null); 
AuthenticationResult authenticationResult = result.get(); 
String token = authenticationResult.getAccessToken(); 

這將導致以下AUTH例外

AuthenticationException: com.microsoft.aad.adal4j.AuthenticationException: {"error":"invalid_client","error_description":"AADSTS70002: Error validating credentials. AADSTS50027: Invalid JWT token. No certificate thumbprint specified in token header.\r\nTrace ID: 9719e621-d8ef-4194-93cd-a78103d5df6b\r\nCorrelation ID: f0300795-fb99-44b2-bd95-8df3975290be\r\nTimestamp: 2016-08-29 13:51:26Z"} 

我不知道如何通過指紋同時呼籲acquireToken。這裏有什麼遺漏嗎?

+0

你能告訴我們你是如何構建'asymmetricKeyCredential'嗎? –

+0

已更新原始文章與完整片段 – YogeshORai

+0

有人可以建議什麼在這裏丟失 – YogeshORai

回答

0

根據您的代碼,您似乎希望使用證書對Azure Service Management API進行身份驗證,但獲取訪問令牌的代碼似乎使用Azure AD進行身份驗證。你可以參考文章Authenticating Service Management Requests瞭解它們的區別。

作爲參考,有一個blog介紹瞭如何在Java中使用帶有證書的Windows Azure服務管理API。

但是,根據我的經驗,根據代碼String AAD_RESOURCE_ID = "https://vault.azure.net";,您似乎還想對Azure密鑰保管庫執行一些管理操作。藉助Azure Key Vault Management的REST API參考,您應該使用Azure資源管理器獲取訪問令牌來執行這些操作。因此,如果您想管理密鑰保管庫,請參閱其他blog以瞭解如何使用Java以ARM進行身份驗證。

希望它有幫助。


更新:

AAD_RESOURCE_ID關鍵庫應該像/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.KeyVault/vaults/{vault-name},請參考文章https://msdn.microsoft.com/en-us/library/azure/mt620025.aspx和搜索關鍵字resource id,看到如下圖所示。 enter image description here

您可以通過Azure CLI命令azure keyvault show --vault-name <your-keyvault-name>獲取資源ID。

相關問題