You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add HTTP authentication mechanism with automatic retry (#3813)
Introduce a flexible mechanism for handling HTTP authentication in HttpClient
with automatic retry support when authentication is required:
- Automatic retry on authentication challenges (e.g., 401 Unauthorized)
- Support for sync (httpAuthentication) and async (httpAuthenticationWhen) flows
- Configurable maximum retry attempts per request
- Authentication retry count tracking via HttpClientRequest/Response
Fixes#3079
Signed-off-by: raccoonback <[email protected]>
For authentication where credentials can be computed immediately without delay. The predicate determines when to retry with authentication. Defaults to 1 maximum retry attempt.
793
+
* {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthentication-java.util.function.BiPredicate-java.util.function.BiConsumer-int-[`httpAuthentication(BiPredicate, BiConsumer, int maxRetries)`] -
794
+
Same as above but allows configuring the maximum count of retry attempts.
For authentication where credentials need to be fetched from external sources or require delayed computation. Returns a `Mono<Void>` to support deferred credential retrieval. Defaults to 1 maximum retry attempt.
797
+
* {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthenticationWhen-java.util.function.BiPredicate-java.util.function.BiFunction-int-[`httpAuthenticationWhen(BiPredicate, BiFunction, int maxRetries)`] -
798
+
Same as above but allows configuring the maximum count of retry attempts.
799
+
800
+
This approach gives you complete control over the authentication flow while Reactor Netty handles the retry mechanism.
801
+
802
+
=== How It Works
803
+
804
+
The typical HTTP authentication flow works as follows:
805
+
806
+
. The client sends an HTTP request to a protected resource.
807
+
. The server responds with an authentication challenge (e.g., `401 Unauthorized` with a `WWW-Authenticate` header).
808
+
. The authenticator function is invoked to add authentication credentials to the request.
809
+
. The request is retried with the authentication credentials.
810
+
. If authentication is successful, the server returns the requested resource.
811
+
812
+
=== Immediate Authentication with httpAuthentication
813
+
814
+
For authentication scenarios where credentials can be computed immediately without needing to fetch them from external sources,
815
+
use {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthentication-java.util.function.BiPredicate-java.util.function.BiConsumer-[`httpAuthentication(BiPredicate, BiConsumer)`].
816
+
This method requires both a predicate to determine when to retry and a consumer to add authentication headers.
817
+
818
+
==== Token-Based Authentication Example
819
+
820
+
The following example demonstrates how to implement Bearer token authentication with configurable retry count:
<1> The predicate checks for `401 Unauthorized` responses to trigger authentication retry.
839
+
<2> The authenticator adds Basic authentication credentials to the `Authorization` header.
840
+
<3> Uses the default maximum retry count of 1.
841
+
842
+
=== Custom Authentication with httpAuthenticationWhen
843
+
844
+
When you need custom retry conditions (e.g., checking specific headers or status codes other than 401),
845
+
use the {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthenticationWhen-java.util.function.BiPredicate-java.util.function.BiFunction-[`httpAuthenticationWhen(BiPredicate, BiFunction)`] method.
846
+
847
+
==== SPNEGO/Negotiate Authentication Example
848
+
849
+
For SPNEGO (Kerberos) authentication, you can implement a custom authenticator using Java's GSS-API:
<2> Use the retry count to implement custom logic (e.g., different token generation).
933
+
<3> Configure maximum authentication retry attempts to 3.
934
+
935
+
NOTE: The retry count starts at `0` for the first authentication attempt and increments by 1 for each subsequent retry.
936
+
When the retry count reaches the configured `maxRetries`, authentication will fail if still unsuccessful.
937
+
938
+
=== Important Notes
939
+
940
+
* The authenticator function is invoked only when the predicate returns `true` (indicating authentication is needed).
941
+
* For `httpAuthentication`, use a `BiConsumer` when credentials can be computed immediately without delay. No `Mono` is needed as headers are added directly.
942
+
* For `httpAuthenticationWhen`, use a `BiFunction` that returns `Mono<Void>` when credentials need to be fetched from external sources or require delayed computation.
943
+
* The authenticator receives the request and remote address, allowing you to customize authentication based on the target server.
944
+
* By default, authentication is retried once per request. You can configure the maximum count of retry attempts using the `maxRetries` parameter. If authentication fails after all retry attempts are exhausted, the error is propagated to the caller.
945
+
* For security reasons, ensure that sensitive credentials are not logged or exposed in error messages.
0 commit comments