BearerTokenAuthenticationPolicy Class
- java.
lang. Object - com.
azure. core. http. policy. BearerTokenAuthenticationPolicy
- com.
Implements
public class BearerTokenAuthenticationPolicy
implements 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");
Constructor Summary
Constructor | Description |
---|---|
BearerTokenAuthenticationPolicy(TokenCredential credential, String[] scopes) |
Creates Bearer |
Method Summary
Modifier and Type | Method and Description |
---|---|
Mono<Void> |
authorizeRequest(HttpPipelineCallContext context)
Executed before sending the initial request and authenticates the request. |
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 appropriate TokenRequestContext to be used for re-authentication. |
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 appropriate TokenRequestContext to be used for re-authentication. |
void |
authorizeRequestSync(HttpPipelineCallContext context)
Synchronously executed before sending the initial request and authenticates the request. |
Mono<Http |
process(HttpPipelineCallContext context, HttpPipelineNextPolicy next)
Processes provided request context and invokes the next policy. |
Http |
processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next)
Processes provided request context and invokes the next policy synchronously. |
Mono<Void> |
setAuthorizationHeader(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext)
Authorizes the request with the bearer token acquired using the specified |
void |
setAuthorizationHeaderSync(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext)
Authorizes the request with the bearer token acquired using the specified |
Methods inherited from java.lang.Object
Constructor Details
BearerTokenAuthenticationPolicy
public BearerTokenAuthenticationPolicy(TokenCredential credential, String[] scopes)
Creates BearerTokenAuthenticationPolicy.
Parameters:
Method Details
authorizeRequest
public Mono
Executed before sending the initial request and authenticates the request.
Parameters:
Returns:
authorizeRequestOnChallenge
public Mono
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 appropriate TokenRequestContext to be used for re-authentication.
The default implementation will attempt to handle Continuous Access Evaluation (CAE) challenges.
Parameters:
Returns:
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 appropriate TokenRequestContext to be used for re-authentication.
The default implementation will attempt to handle Continuous Access Evaluation (CAE) challenges.
Parameters:
Returns:
authorizeRequestSync
public void authorizeRequestSync(HttpPipelineCallContext context)
Synchronously executed before sending the initial request and authenticates the request.
Parameters:
process
public Mono
Processes provided request context and invokes the next policy.
Parameters:
processSync
public HttpResponse processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next)
Processes provided request context and invokes the next policy synchronously.
Parameters:
setAuthorizationHeader
public Mono
Authorizes the request with the bearer token acquired using the specified tokenRequestContext
Parameters:
Returns:
setAuthorizationHeaderSync
public void setAuthorizationHeaderSync(HttpPipelineCallContext context, TokenRequestContext tokenRequestContext)
Authorizes the request with the bearer token acquired using the specified tokenRequestContext
Parameters:
Applies to
Azure SDK for Java