Skip to content

Commit dead58f

Browse files
committed
Update documentation and examples for httpAuthentication API changes
Signed-off-by: raccoonback <[email protected]>
1 parent 721be5c commit dead58f

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
@@ -783,11 +783,12 @@ include::{examples-dir}/resolver/Application.java[lines=18..39]
783783
Reactor Netty `HttpClient` provides a flexible HTTP authentication framework that allows you to implement
784784
custom authentication mechanisms such as SPNEGO/Negotiate, OAuth, Bearer tokens, or any other HTTP-based authentication scheme.
785785

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

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

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

@@ -801,19 +802,39 @@ The typical HTTP authentication flow works as follows:
801802
. The request is retried with the authentication credentials.
802803
. If authentication is successful, the server returns the requested resource.
803804

804-
=== Token-Based Authentication Example
805+
=== Simple Authentication with httpAuthentication
806+
807+
For most authentication scenarios where you want to retry on `401 Unauthorized` responses, use the simpler
808+
{javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthentication-java.util.function.BiFunction-[`httpAuthentication(BiFunction)`] method.
809+
810+
==== Token-Based Authentication Example
805811

806812
The following example demonstrates how to implement Bearer token authentication:
807813

808814
{examples-link}/authentication/token/Application.java
809815
[%unbreakable]
810816
----
811-
include::{examples-dir}/authentication/token/Application.java[lines=18..52]
817+
include::{examples-dir}/authentication/token/Application.java[lines=18..51]
812818
----
813-
<1> The predicate checks if the response status is `401 Unauthorized`.
819+
<1> Automatically retries on `401 Unauthorized` responses.
814820
<2> The authenticator adds the `Authorization` header with a Bearer token.
815821

816-
=== SPNEGO/Negotiate Authentication Example
822+
==== Basic Authentication Example
823+
824+
{examples-link}/authentication/basic/Application.java
825+
[%unbreakable]
826+
----
827+
include::{examples-dir}/authentication/basic/Application.java[lines=18..45]
828+
----
829+
<1> Automatically retries on `401 Unauthorized` responses.
830+
<2> The authenticator adds Basic authentication credentials to the `Authorization` header.
831+
832+
=== Custom Authentication with httpAuthenticationWhen
833+
834+
When you need custom retry conditions (e.g., checking specific headers or status codes other than 401),
835+
use the {javadoc}/reactor/netty/http/client/HttpClient.html#httpAuthenticationWhen-java.util.function.BiPredicate-java.util.function.BiFunction-[`httpAuthenticationWhen(BiPredicate, BiFunction)`] method.
836+
837+
==== SPNEGO/Negotiate Authentication Example
817838

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

@@ -822,7 +843,7 @@ For SPNEGO (Kerberos) authentication, you can implement a custom authenticator u
822843
----
823844
include::{examples-dir}/authentication/spnego/Application.java[lines=18..69]
824845
----
825-
<1> The predicate checks for `401 Unauthorized` with `WWW-Authenticate: Negotiate` header.
846+
<1> Custom predicate checks for `401 Unauthorized` with `WWW-Authenticate: Negotiate` header.
826847
<2> The authenticator generates a SPNEGO token using GSS-API and adds it to the `Authorization` header.
827848

828849
NOTE: For SPNEGO authentication, you need to configure Kerberos settings (e.g., `krb5.conf`) and JAAS configuration
@@ -831,14 +852,13 @@ to point to your configuration files.
831852

832853
=== Custom Authentication Scenarios
833854

834-
The `httpAuthentication` method is flexible enough to support various authentication scenarios:
855+
The authentication framework is flexible enough to support various authentication scenarios:
835856

836857
==== OAuth 2.0 Authentication
837858
[source,java]
838859
----
839860
HttpClient client = HttpClient.create()
840861
.httpAuthentication(
841-
(req, res) -> res.status().code() == 401,
842862
(req, addr) -> {
843863
return fetchOAuthToken() // <1>
844864
.doOnNext(token ->
@@ -849,20 +869,11 @@ HttpClient client = HttpClient.create()
849869
----
850870
<1> Asynchronously fetch an OAuth token and add it to the request.
851871

852-
==== Basic Authentication
853-
{examples-link}/authentication/basic/Application.java
854-
[%unbreakable]
855-
----
856-
include::{examples-dir}/authentication/basic/Application.java[lines=18..46]
857-
----
858-
<1> The predicate checks if the response status is `401 Unauthorized`.
859-
<2> The authenticator adds Basic authentication credentials to the `Authorization` header.
860-
861872
==== Proxy Authentication
862873
[source,java]
863874
----
864875
HttpClient client = HttpClient.create()
865-
.httpAuthentication(
876+
.httpAuthenticationWhen(
866877
(req, res) -> res.status().code() == 407, // <1>
867878
(req, addr) -> {
868879
String proxyCredentials = generateProxyCredentials();
@@ -871,11 +882,11 @@ HttpClient client = HttpClient.create()
871882
}
872883
);
873884
----
874-
<1> Check for `407 Proxy Authentication Required` status code.
885+
<1> Custom predicate checks for `407 Proxy Authentication Required` status code.
875886

876887
=== Important Notes
877888

878-
* The authenticator function is invoked only when the predicate returns `true`.
889+
* The authenticator function is invoked only when authentication is needed (on `401` for `httpAuthentication`, or when the predicate returns `true` for `httpAuthenticationWhen`).
879890
* The authenticator receives the request and remote address, allowing you to customize authentication based on the target server.
880891
* The authenticator returns a `Mono<Void>` which allows for asynchronous credential retrieval.
881892
* 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)