I am trying to get secret from azure keyvault. When i run class independently from main method it returns secret but when i integrated this same code in servlet application at code line future.get(); here it gets blocks it do not proceed further like a deadlock it keeps waiting and sometime get java.util.concurrent.ExecutionException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List and com.microsoft.azure.keyvault.models.KeyVaultErrorException: Status code 401, {"error":{"code":"Unauthorized","message":"AKV10001: Unable to parse JWT token: expected 3 compact JSON elements, found 1."}} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) .Please help me on this.
public class KeyVaultTest {
private static AuthenticationResult getAccessToken(String authorization, String resource) throws InterruptedException, ExecutionException, MalformedURLException {
String clientId = "XXX"; // Client ID
String clientSecret = "XXX"; //Client Secret
AuthenticationResult result = null;
//Starts a service to fetch access token.
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authorization, false, service);
Future<AuthenticationResult> future = null;
//Acquires token based on client ID and client secret.
if (clientSecret != null && clientSecret != null) {
ClientCredential credentials = new ClientCredential(clientId, clientSecret);
future = context.acquireToken(resource, credentials, null);
}
result = future.get();
} finally {
service.shutdown();
}
if (result == null) {
throw new RuntimeException("Authentication results were null.");
}
return result;
}
public static void main(String[] args) {
String vaultBase = "https://myapp-keyvault.vault.azure.net/";
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultCredentials(){
@Override
public String doAuthenticate(String authorization, String resource, String scope) {
String token = null;
try {
AuthenticationResult authResult = getAccessToken(authorization, resource);
token = authResult.getAccessToken();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(token);
return token;
}
});
SecretBundle secretBundle = keyVaultClient.getSecret(vaultBase, "abc");
System.out.println(secretBundle.value());
}