Skip to content

Commit 56e09a1

Browse files
committed
Fix class name; edit for clarity, progressive structure
1 parent 0afeb47 commit 56e09a1

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

docs/reference/transport/rest5-client/config/basic_authentication.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Basic authentication
33

4-
Configuring basic authentication can be done by providing an `HttpClientConfigCallback` while building the `RestClient` through its builder. The interface has one method that receives an instance of [`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`](https://hc.apache.org/httpcomponents-asyncclient-4.1.x/current/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html) as an argument and has the same return type. The http client builder can be modified and then returned. In the following example we set a default credentials provider that requires basic authentication.
4+
To use basic authentication in the REST 5 client, set a default authorization header:
55

66
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-basic-auth
77
```java
@@ -16,7 +16,9 @@ Rest5ClientBuilder restClient = Rest5Client
1616

1717
```
1818

19-
Preemptive Authentication can be disabled, which means that every request will be sent without authorization headers to see if it is accepted and, upon receiving an HTTP 401 response, it will resend the exact same request with the basic authentication header. If you wish to do this, then you can do so by disabling it via the `HttpClientConfigCallback`:
19+
To configure authentication behavior, pass an `HttpClientConfigCallback` when building the `Rest5Client`. The callback's single method takes an instance of [`org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder`](https://hc.apache.org/httpcomponents-client-5.5.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.html) as an argument and returns the same type, so you can modify the provided builder and return it for the client to use.
20+
21+
By default, the HTTP client uses preemptive authentication: it includes credentials in the initial request. You might want to use non-preemptive authentication, which sends a request without credentials and retries with the header after a `401 Unauthorized` challenge. To do this, set an `HttpClientConfigCallback` with auth caching disabled:
2022

2123
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-disable-preemptive-auth
2224
```java
@@ -26,12 +28,12 @@ var creds = Base64.getEncoder().encodeToString("user:test-user-password".getByte
2628

2729
Rest5ClientBuilder restClient = Rest5Client
2830
.builder(new HttpHost("https", "localhost", 9200))
29-
.setHttpClientConfigCallback(HttpAsyncClientBuilder::disableAuthCaching)
31+
.setHttpClientConfigCallback(HttpAsyncClientBuilder::disableAuthCaching) <1>
3032
.setDefaultHeaders(new Header[]{
3133
new BasicHeader("Authorization", "Basic " + creds)
3234
});
3335
```
3436

35-
1. Disable preemptive authentication
36-
37+
1. Disables preemptive authentication
3738

39+
For more options, refer to [](other_authentication_methods.md).

0 commit comments

Comments
 (0)