Package com.azure.identity
Class AuthenticationUtil
java.lang.Object
com.azure.identity.AuthenticationUtil
Utility methods for working with authentication.
-
Method Summary
Modifier and TypeMethodDescriptiongetBearerTokenSupplier
(com.azure.core.credential.TokenCredential credential, String... scopes) Creates aSupplier
that provides a Bearer token from the specified credential.
-
Method Details
-
getBearerTokenSupplier
public static Supplier<String> getBearerTokenSupplier(com.azure.core.credential.TokenCredential credential, String... scopes) Creates aSupplier
that provides a Bearer token from the specified credential. The token is cached and will refresh when it expires.Using the supplier:
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build(); String scope = "https://cognitiveservices.azure.com/.default"; Supplier<String> supplier = AuthenticationUtil.getBearerTokenSupplier(credential, scope); // This example simply uses the Azure SDK HTTP library to demonstrate setting the header. // Use the token as is appropriate for your circumstances. HttpRequest request = new HttpRequest(HttpMethod.GET, "https://www.example.com"); request.setHeader(HttpHeaderName.AUTHORIZATION, "Bearer " + supplier.get()); HttpClient client = HttpClient.createDefault(); client.sendSync(request, Context.NONE);
-