Skip to content

Commit 17fbdbe

Browse files
committed
Update documentation and examples for httpAuthentication API changes
Signed-off-by: raccoonback <[email protected]>
1 parent 05d9166 commit 17fbdbe

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

docs/modules/ROOT/pages/http-client.adoc

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,12 @@ include::{examples-dir}/resolver/Application.java[lines=18..39]
748748
Reactor Netty `HttpClient` provides a flexible HTTP authentication framework that allows you to implement
749749
custom authentication mechanisms such as SPNEGO/Negotiate, OAuth, Bearer tokens, or any other HTTP-based authentication scheme.
750750

751-
The {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthentication-java.util.function.BiPredicate-java.util.function.BiFunction-[`httpAuthentication`]
752-
method accepts two parameters:
751+
The framework provides two APIs for HTTP authentication:
753752

754-
* A predicate that determines when authentication should be applied (typically by checking the HTTP status code and headers)
755-
* An authenticator function that applies authentication credentials to the request
753+
* {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthentication-java.util.function.BiFunction-[`httpAuthentication(BiFunction)`] -
754+
Automatically retries requests when the server returns `401 Unauthorized`.
755+
* {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthenticationWhen-java.util.function.BiPredicate-java.util.function.BiFunction-[`httpAuthenticationWhen(BiPredicate, BiFunction)`] -
756+
Allows custom retry conditions based on request and response.
756757

757758
This approach gives you complete control over the authentication flow while Reactor Netty handles the retry mechanism.
758759

@@ -766,19 +767,39 @@ The typical HTTP authentication flow works as follows:
766767
. The request is retried with the authentication credentials.
767768
. If authentication is successful, the server returns the requested resource.
768769

769-
=== Token-Based Authentication Example
770+
=== Simple Authentication with httpAuthentication
771+
772+
For most authentication scenarios where you want to retry on `401 Unauthorized` responses, use the simpler
773+
{javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthentication-java.util.function.BiFunction-[`httpAuthentication(BiFunction)`] method.
774+
775+
==== Token-Based Authentication Example
770776

771777
The following example demonstrates how to implement Bearer token authentication:
772778

773779
{examples-link}/authentication/token/Application.java
774780
[%unbreakable]
775781
----
776-
include::{examples-dir}/authentication/token/Application.java[lines=18..52]
782+
include::{examples-dir}/authentication/token/Application.java[lines=18..51]
777783
----
778-
<1> The predicate checks if the response status is `401 Unauthorized`.
784+
<1> Automatically retries on `401 Unauthorized` responses.
779785
<2> The authenticator adds the `Authorization` header with a Bearer token.
780786

781-
=== SPNEGO/Negotiate Authentication Example
787+
==== Basic Authentication Example
788+
789+
{examples-link}/authentication/basic/Application.java
790+
[%unbreakable]
791+
----
792+
include::{examples-dir}/authentication/basic/Application.java[lines=18..45]
793+
----
794+
<1> Automatically retries on `401 Unauthorized` responses.
795+
<2> The authenticator adds Basic authentication credentials to the `Authorization` header.
796+
797+
=== Custom Authentication with httpAuthenticationWhen
798+
799+
When you need custom retry conditions (e.g., checking specific headers or status codes other than 401),
800+
use the {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthenticationWhen-java.util.function.BiPredicate-java.util.function.BiFunction-[`httpAuthenticationWhen(BiPredicate, BiFunction)`] method.
801+
802+
==== SPNEGO/Negotiate Authentication Example
782803

783804
For SPNEGO (Kerberos) authentication, you can implement a custom authenticator using Java's GSS-API:
784805

@@ -787,7 +808,7 @@ For SPNEGO (Kerberos) authentication, you can implement a custom authenticator u
787808
----
788809
include::{examples-dir}/authentication/spnego/Application.java[lines=18..69]
789810
----
790-
<1> The predicate checks for `401 Unauthorized` with `WWW-Authenticate: Negotiate` header.
811+
<1> Custom predicate checks for `401 Unauthorized` with `WWW-Authenticate: Negotiate` header.
791812
<2> The authenticator generates a SPNEGO token using GSS-API and adds it to the `Authorization` header.
792813

793814
NOTE: For SPNEGO authentication, you need to configure Kerberos settings (e.g., `krb5.conf`) and JAAS configuration
@@ -796,14 +817,13 @@ to point to your configuration files.
796817

797818
=== Custom Authentication Scenarios
798819

799-
The `httpAuthentication` method is flexible enough to support various authentication scenarios:
820+
The authentication framework is flexible enough to support various authentication scenarios:
800821

801822
==== OAuth 2.0 Authentication
802823
[source,java]
803824
----
804825
HttpClient client = HttpClient.create()
805826
.httpAuthentication(
806-
(req, res) -> res.status().code() == 401,
807827
(req, addr) -> {
808828
return fetchOAuthToken() // <1>
809829
.doOnNext(token ->
@@ -814,20 +834,11 @@ HttpClient client = HttpClient.create()
814834
----
815835
<1> Asynchronously fetch an OAuth token and add it to the request.
816836

817-
==== Basic Authentication
818-
{examples-link}/authentication/basic/Application.java
819-
[%unbreakable]
820-
----
821-
include::{examples-dir}/authentication/basic/Application.java[lines=18..46]
822-
----
823-
<1> The predicate checks if the response status is `401 Unauthorized`.
824-
<2> The authenticator adds Basic authentication credentials to the `Authorization` header.
825-
826837
==== Proxy Authentication
827838
[source,java]
828839
----
829840
HttpClient client = HttpClient.create()
830-
.httpAuthentication(
841+
.httpAuthenticationWhen(
831842
(req, res) -> res.status().code() == 407, // <1>
832843
(req, addr) -> {
833844
String proxyCredentials = generateProxyCredentials();
@@ -836,11 +847,11 @@ HttpClient client = HttpClient.create()
836847
}
837848
);
838849
----
839-
<1> Check for `407 Proxy Authentication Required` status code.
850+
<1> Custom predicate checks for `407 Proxy Authentication Required` status code.
840851

841852
=== Important Notes
842853

843-
* The authenticator function is invoked only when the predicate returns `true`.
854+
* The authenticator function is invoked only when authentication is needed (on `401` for `httpAuthentication`, or when the predicate returns `true` for `httpAuthenticationWhen`).
844855
* The authenticator receives the request and remote address, allowing you to customize authentication based on the target server.
845856
* The authenticator returns a `Mono<Void>` which allows for asynchronous credential retrieval.
846857
* Authentication is retried only once per request. If authentication fails after retry, the error is propagated to the caller.

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/authentication/basic/Application.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public class Application {
2727
public static void main(String[] args) {
2828
HttpClient client =
2929
HttpClient.create()
30-
.httpAuthentication(
31-
(req, res) -> res.status().code() == 401, // <1>
30+
.httpAuthentication( // <1>
3231
(req, addr) -> { // <2>
3332
String credentials = "username:password";
3433
String encodedCredentials = Base64.getEncoder()

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/authentication/spnego/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class Application {
3232
public static void main(String[] args) {
3333
HttpClient client =
3434
HttpClient.create()
35-
.httpAuthentication(
35+
.httpAuthenticationWhen(
3636
(req, res) -> res.status().code() == 401 && // <1>
3737
res.responseHeaders().contains("WWW-Authenticate", "Negotiate", true),
3838
(req, addr) -> { // <2>

reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/http/client/authentication/token/Application.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public class Application {
2626
public static void main(String[] args) {
2727
HttpClient client =
2828
HttpClient.create()
29-
.httpAuthentication(
30-
(req, res) -> res.status().code() == 401, // <1>
29+
.httpAuthentication( // <1>
3130
(req, addr) -> { // <2>
3231
String token = generateAuthToken(addr);
3332
req.header(HttpHeaderNames.AUTHORIZATION, "Bearer " + token);

0 commit comments

Comments
 (0)