-
Notifications
You must be signed in to change notification settings - Fork 688
Support HTTP Authentication in HttpClient #3813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raccoonback
wants to merge
17
commits into
reactor:main
Choose a base branch
from
raccoonback:issue-3079
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+813
−44
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4ddffb0
Support SPNEGO (Kerberos) authentication
raccoonback 6b09c79
Reuse SPNEGO token until expiry and reset on expiration
raccoonback 5a8ff02
Ensure GSSContext is disposed after initializing security context
raccoonback 956b1f8
Add SpnegoAuthenticationException for better error handling in SPNEGO…
raccoonback 82dd4f6
Improve SPNEGO authentication retry mechanism
raccoonback d1ef964
Support GSSCredential-based SPNEGO authentication
raccoonback 5e664fa
Generalize the httpAuthentication infrastructure and remove SPNEGO-sp…
raccoonback ee921a0
Update omitting the stack trace
raccoonback e35a028
Apply authenticator after HTTP request preparation
raccoonback 0e05222
Ensure response messages are drained before authentication retry
raccoonback 1865e7c
Extend authentication handling to WebSocket operations
raccoonback 138d7d2
Add httpAuthentication API and rename existing to httpAuthenticationWhen
raccoonback 721be5c
Add test for HTTP authentication state propagation
raccoonback dead58f
Update documentation and examples for httpAuthentication API changes
raccoonback 723ac4b
Fix broken test
raccoonback d2c310c
Fix checkstyle
raccoonback b65d57d
Fix test warning log
raccoonback File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...va/reactor/netty/examples/documentation/http/client/authentication/basic/Application.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package reactor.netty.examples.documentation.http.client.authentication.basic; | ||
|
|
||
| import io.netty.handler.codec.http.HttpHeaderNames; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.netty.http.client.HttpClient; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| HttpClient client = | ||
| HttpClient.create() | ||
| .httpAuthentication(// <1> | ||
| (req, addr) -> { // <2> | ||
| String credentials = "username:password"; | ||
| String encodedCredentials = Base64.getEncoder() | ||
| .encodeToString(credentials.getBytes(StandardCharsets.UTF_8)); | ||
| req.header(HttpHeaderNames.AUTHORIZATION, "Basic " + encodedCredentials); | ||
| return Mono.empty(); | ||
| } | ||
| ); | ||
|
|
||
| client.get() | ||
| .uri("https://example.com/") | ||
| .response() | ||
| .block(); | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
...a/reactor/netty/examples/documentation/http/client/authentication/spnego/Application.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package reactor.netty.examples.documentation.http.client.authentication.spnego; | ||
|
|
||
| import io.netty.handler.codec.http.HttpHeaderNames; | ||
| import org.ietf.jgss.GSSContext; | ||
| import org.ietf.jgss.GSSException; | ||
| import org.ietf.jgss.GSSManager; | ||
| import org.ietf.jgss.GSSName; | ||
| import org.ietf.jgss.Oid; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.netty.http.client.HttpClient; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.Base64; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| HttpClient client = | ||
| HttpClient.create() | ||
| .httpAuthenticationWhen( | ||
| (req, res) -> res.status().code() == 401 && // <1> | ||
| res.responseHeaders().contains("WWW-Authenticate", "Negotiate", true), | ||
| (req, addr) -> { // <2> | ||
| try { | ||
| GSSManager manager = GSSManager.getInstance(); | ||
| String hostName = ((InetSocketAddress) addr).getHostString(); | ||
| String serviceName = "HTTP@" + hostName; | ||
| GSSName serverName = manager.createName(serviceName, GSSName.NT_HOSTBASED_SERVICE); | ||
|
|
||
| Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2"); | ||
| GSSContext context = manager.createContext( | ||
| serverName, krb5Mechanism, null, GSSContext.DEFAULT_LIFETIME); | ||
|
|
||
| byte[] token = context.initSecContext(new byte[0], 0, 0); | ||
| String encodedToken = Base64.getEncoder().encodeToString(token); | ||
|
|
||
| req.header(HttpHeaderNames.AUTHORIZATION, "Negotiate " + encodedToken); | ||
|
|
||
| context.dispose(); | ||
| } | ||
| catch (GSSException e) { | ||
| return Mono.error(new RuntimeException( | ||
| "Failed to generate SPNEGO token", e)); | ||
| } | ||
| return Mono.empty(); | ||
| } | ||
| ); | ||
|
|
||
| client.get() | ||
| .uri("https://example.com/") | ||
| .response() | ||
| .block(); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
...va/reactor/netty/examples/documentation/http/client/authentication/token/Application.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package reactor.netty.examples.documentation.http.client.authentication.token; | ||
|
|
||
| import io.netty.handler.codec.http.HttpHeaderNames; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.netty.http.client.HttpClient; | ||
|
|
||
| import java.net.SocketAddress; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| HttpClient client = | ||
| HttpClient.create() | ||
| .httpAuthentication(// <1> | ||
| (req, addr) -> { // <2> | ||
| String token = generateAuthToken(addr); | ||
| req.header(HttpHeaderNames.AUTHORIZATION, "Bearer " + token); | ||
| return Mono.empty(); | ||
| } | ||
| ); | ||
|
|
||
| client.get() | ||
| .uri("https://example.com/") | ||
| .response() | ||
| .block(); | ||
| } | ||
|
|
||
| /** | ||
| * Generates an authentication token for the given remote address. | ||
| * In a real application, this would retrieve or generate a valid token. | ||
| */ | ||
| static String generateAuthToken(SocketAddress remoteAddress) { | ||
| // In a real application, implement token generation/retrieval logic | ||
| return "sample-token-123"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Useless parameter Note documentation