Class BearerTokenAuthenticationPolicy
- All Implemented Interfaces:
HttpPipelinePolicy
The BearerTokenAuthenticationPolicy class is an implementation of the HttpPipelinePolicy interface.
This policy uses a TokenCredential to authenticate the request with a bearer token.
This class is useful when you need to authorize requests with a bearer token from Azure. It ensures that the requests are sent over HTTPS to prevent the token from being leaked.
Code sample:
In this example, a BearerTokenAuthenticationPolicy is created with a TokenCredential and a scope.
The policy can then added to the pipeline. The request sent via the pipeline will then include the
Authorization header with the bearer token.
TokenCredential credential = new BasicAuthenticationCredential("username", "password");
BearerTokenAuthenticationPolicy policy = new BearerTokenAuthenticationPolicy(credential,
"https://management.azure.com/.default");
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBearerTokenAuthenticationPolicy(TokenCredential credential, String... scopes) Creates BearerTokenAuthenticationPolicy. -
Method Summary
Modifier and TypeMethodDescriptionauthorizeRequest(HttpPipelineCallContext context) Executed before sending the initial request and authenticates the request.authorizeRequestOnChallenge(HttpPipelineCallContext context, HttpResponse response) Handles the authentication challenge in the event a 401 response with a WWW-Authenticate authentication challenge header is received after the initial request and returns appropriateTokenRequestContextto be used for re-authentication.booleanauthorizeRequestOnChallengeSync(HttpPipelineCallContext context, HttpResponse response) Handles the authentication challenge in the event a 401 response with a WWW-Authenticate authentication challenge header is received after the initial request and returns appropriateTokenRequestContextto be used for re-authentication.voidSynchronously executed before sending the initial request and authenticates the request.process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) Processes provided request context and invokes the next policy.processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next) Processes provided request context and invokes the next policy synchronously.setAuthorizationHeader(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext) Authorizes the request with the bearer token acquired using the specifiedtokenRequestContextvoidsetAuthorizationHeaderSync(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext) Authorizes the request with the bearer token acquired using the specifiedtokenRequestContextMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.azure.core.http.policy.HttpPipelinePolicy
getPipelinePosition
-
Constructor Details
-
BearerTokenAuthenticationPolicy
Creates BearerTokenAuthenticationPolicy.- Parameters:
credential- the token credential to authenticate the requestscopes- the scopes of authentication the credential should get token for
-
-
Method Details
-
authorizeRequest
Executed before sending the initial request and authenticates the request. -
authorizeRequestSync
Synchronously executed before sending the initial request and authenticates the request.- Parameters:
context- The request context.
-
authorizeRequestOnChallenge
public Mono<Boolean> authorizeRequestOnChallenge(HttpPipelineCallContext context, HttpResponse response) Handles the authentication challenge in the event a 401 response with a WWW-Authenticate authentication challenge header is received after the initial request and returns appropriateTokenRequestContextto be used for re-authentication.The default implementation will attempt to handle Continuous Access Evaluation (CAE) challenges.
- Parameters:
context- The request context.response- The Http Response containing the authentication challenge header.- Returns:
- A
MonocontainingTokenRequestContext
-
authorizeRequestOnChallengeSync
public boolean authorizeRequestOnChallengeSync(HttpPipelineCallContext context, HttpResponse response) Handles the authentication challenge in the event a 401 response with a WWW-Authenticate authentication challenge header is received after the initial request and returns appropriateTokenRequestContextto be used for re-authentication.The default implementation will attempt to handle Continuous Access Evaluation (CAE) challenges.
- Parameters:
context- The request context.response- The Http Response containing the authentication challenge header.- Returns:
- A boolean indicating if containing the
TokenRequestContextfor re-authentication
-
process
Description copied from interface:HttpPipelinePolicyProcesses provided request context and invokes the next policy.- Specified by:
processin interfaceHttpPipelinePolicy- Parameters:
context- The request context.next- The next policy to invoke.- Returns:
- A publisher that initiates the request upon subscription and emits a response on completion.
-
processSync
Description copied from interface:HttpPipelinePolicyProcesses provided request context and invokes the next policy synchronously.- Specified by:
processSyncin interfaceHttpPipelinePolicy- Parameters:
context- The request context.next- The next policy to invoke.- Returns:
- A publisher that initiates the request upon subscription and emits a response on completion.
-
setAuthorizationHeader
public Mono<Void> setAuthorizationHeader(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext) Authorizes the request with the bearer token acquired using the specifiedtokenRequestContext -
setAuthorizationHeaderSync
public void setAuthorizationHeaderSync(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext) Authorizes the request with the bearer token acquired using the specifiedtokenRequestContext- Parameters:
context- the HTTP pipeline context.tokenRequestContext- the token request context to be used for token acquisition.
-