Package com.azure.core.http.policy
Class KeyCredentialPolicy
java.lang.Object
com.azure.core.http.policy.KeyCredentialPolicy
- All Implemented Interfaces:
HttpPipelinePolicy
- Direct Known Subclasses:
AzureKeyCredentialPolicy
The
KeyCredentialPolicy class is an implementation of the HttpPipelinePolicy interface. This policy
uses a KeyCredential to set the authorization key for a request in the form of a header.
This class is useful when you need to authorize requests with a key. It ensures that the requests are sent over HTTPS to prevent the key from being leaked. The key is set in the header of the HTTP request.
Code sample:
In this example, a KeyCredentialPolicy is created with a key and a header name. The policy can then be
added to the pipeline. The request sent by the pipeline will then include the specified header with the key as its
value.
KeyCredential credential = new KeyCredential("my_key");
KeyCredentialPolicy policy = new KeyCredentialPolicy("my_header", credential);
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionKeyCredentialPolicy(String name, KeyCredential credential) Creates a policy that uses the passedKeyCredentialto set the specified header name.KeyCredentialPolicy(String name, KeyCredential credential, String prefix) Creates a policy that uses the passedKeyCredentialto set the specified header name. -
Method Summary
Modifier and TypeMethodDescriptionprocess(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.Methods 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
-
KeyCredentialPolicy
Creates a policy that uses the passedKeyCredentialto set the specified header name.- Parameters:
name- The name of the key header that will be set toKeyCredential.getKey().credential- TheKeyCredentialcontaining the authorization key to use.- Throws:
NullPointerException- Ifnameorcredentialisnull.IllegalArgumentException- Ifnameis empty.
-
KeyCredentialPolicy
Creates a policy that uses the passedKeyCredentialto set the specified header name.The
prefixwill be applied before theKeyCredential.getKey()when setting the header. A space will be inserted betweenprefixand credential.- Parameters:
name- The name of the key header that will be set toKeyCredential.getKey().credential- TheKeyCredentialcontaining the authorization key to use.prefix- The prefix to apply before the credential, for example "SharedAccessKey credential".- Throws:
NullPointerException- Ifnameorcredentialisnull.IllegalArgumentException- Ifnameis empty.
-
-
Method Details
-
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.
-