This is an automated email from the ASF dual-hosted git repository.
Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 7291bc1fd672 CAMEL-27017: camel-http - endpoint authMethod=Bearer
sends the token, and authMethod=NTLM works without authDomain
7291bc1fd672 is described below
commit 7291bc1fd672f370ead993626b0e3733be6622d4
Author: croway <[email protected]>
AuthorDate: Mon Nov 16 10:10:12 2026 +0100
CAMEL-27017: camel-http - endpoint authMethod=Bearer sends the token, and
authMethod=NTLM works without authDomain
The endpoint registered only authUsername and authPassword, so a Bearer
challenge was not answered. The preemptive handler cached a BearerScheme
without its token, and every request failed in the client with "Bearer
token is null". authMethod=NTLM selected NTLM only with authDomain, and
the component configuration ignored it.
Register the bearer token scoped to the endpoint host, complete the
preemptive BearerScheme with its token before caching it, and pass an
explicit NTLM flag to DefaultAuthenticationHttpClientConfigurer.
Document authentication on the http component page.
---
.../org/apache/camel/catalog/components/http.json | 2 +-
.../org/apache/camel/catalog/components/https.json | 2 +-
.../apache/camel/catalog/docs/http-component.adoc | 69 +++++-
.../org/apache/camel/component/http/http.json | 2 +-
.../org/apache/camel/component/http/https.json | 2 +-
.../camel-http/src/main/docs/http-component.adoc | 69 +++++-
.../DefaultAuthenticationHttpClientConfigurer.java | 11 +-
.../apache/camel/component/http/HttpComponent.java | 21 +-
.../apache/camel/component/http/HttpEndpoint.java | 6 +-
.../http/PreemptiveAuthExecChainHandler.java | 21 +-
.../http/HttpBearerAndNtlmAuthenticationTest.java | 239 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 10 +
.../endpoint/dsl/HttpEndpointBuilderFactory.java | 10 +-
13 files changed, 438 insertions(+), 26 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/http.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/http.json
index 5ba15d9f45ba..85ef1dc6fc30 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/http.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/http.json
@@ -151,7 +151,7 @@
"proxyPort": { "index": 48, "kind": "parameter", "displayName": "Proxy
Port", "group": "proxy", "label": "producer,proxy", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "description": "Proxy server port" },
"authBearerToken": { "index": 49, "kind": "parameter", "displayName":
"Auth Bearer Token", "group": "security", "label": "producer,security",
"required": false, "type": "string", "javaType": "java.lang.String",
"deprecated": false, "autowired": false, "secret": true, "security": "secret",
"description": "Authentication bearer token" },
"authDomain": { "index": 50, "kind": "parameter", "displayName": "Auth
Domain", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication domain to
use with NTLM" },
- "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
preemptive basic authentication to the server." },
+ "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
the Basic credentials or the Bearer token with the first request, without
waiting for the server to ask for them with a 401 response." },
"authHost": { "index": 52, "kind": "parameter", "displayName": "Auth
Host", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication host to use
with NTLM" },
"authMethod": { "index": 53, "kind": "parameter", "displayName": "Auth
Method", "group": "security", "label": "producer,security", "required": false,
"type": "enum", "javaType": "java.lang.String", "enum": [ "Basic", "Bearer",
"NTLM" ], "deprecated": false, "autowired": false, "secret": false,
"description": "The authentication method to use: Basic, Bearer or NTLM. (NTLM
is deprecated)" },
"authPassword": { "index": 54, "kind": "parameter", "displayName": "Auth
Password", "group": "security", "label": "producer,security", "required":
false, "type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": true, "security": "secret", "description":
"Authentication password" },
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/https.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/https.json
index 9280f47dd985..56a3697f31d9 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/https.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/https.json
@@ -151,7 +151,7 @@
"proxyPort": { "index": 48, "kind": "parameter", "displayName": "Proxy
Port", "group": "proxy", "label": "producer,proxy", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "description": "Proxy server port" },
"authBearerToken": { "index": 49, "kind": "parameter", "displayName":
"Auth Bearer Token", "group": "security", "label": "producer,security",
"required": false, "type": "string", "javaType": "java.lang.String",
"deprecated": false, "autowired": false, "secret": true, "security": "secret",
"description": "Authentication bearer token" },
"authDomain": { "index": 50, "kind": "parameter", "displayName": "Auth
Domain", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication domain to
use with NTLM" },
- "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
preemptive basic authentication to the server." },
+ "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
the Basic credentials or the Bearer token with the first request, without
waiting for the server to ask for them with a 401 response." },
"authHost": { "index": 52, "kind": "parameter", "displayName": "Auth
Host", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication host to use
with NTLM" },
"authMethod": { "index": 53, "kind": "parameter", "displayName": "Auth
Method", "group": "security", "label": "producer,security", "required": false,
"type": "enum", "javaType": "java.lang.String", "enum": [ "Basic", "Bearer",
"NTLM" ], "deprecated": false, "autowired": false, "secret": false,
"description": "The authentication method to use: Basic, Bearer or NTLM. (NTLM
is deprecated)" },
"authPassword": { "index": 54, "kind": "parameter", "displayName": "Auth
Password", "group": "security", "label": "producer,security", "required":
false, "type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": true, "security": "secret", "description":
"Authentication password" },
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
index 0b58a0d04d6a..a93916cd243d 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
@@ -740,10 +740,73 @@ Most of the option are available here:
https://hc.apache.org/httpcomponents-clie
To disable cookies in the CookieStore, you can set the HTTP Client to ignore
cookies by
adding this URI option: `httpClient.cookieSpec=ignore`. This doesn't affect
cookies manually set in the `Cookie` header
-=== Basic auth with the streaming message body
+=== Authentication
-To avoid the `NonRepeatableRequestException`, you need to do the
-Preemptive Basic Authentication by adding the option:
`authenticationPreemptive=true`
+The HTTP producer authenticates with the options of the endpoint. To use the
same settings for all the endpoints of a
+component, set them in an `HttpConfiguration` given to the `httpConfiguration`
option of the component.
+
+[cols="1,3"]
+|===
+| Method | Options
+
+| Basic
+| `authUsername` and `authPassword`. `authMethod=Basic` is optional.
+
+| Bearer
+| `authMethod=Bearer` and `authBearerToken`.
+
+| NTLM (deprecated)
+| `authMethod=NTLM`, `authUsername`, `authPassword`, and `authDomain` if the
account is in a domain.
+|===
+
+By default, the credentials are sent only when the server answers with `401
Unauthorized` and a `WWW-Authenticate`
+header that asks for them. With `authenticationPreemptive=true`, the Basic
credentials or the Bearer token are sent
+with the first request. Use it when the server does not send a challenge, or
when the message body is a stream that
+can be sent only once: otherwise the request fails with
`NonRepeatableRequestException` when the server asks for the
+credentials.
+
+When `followRedirects=true`, a redirect to another host does not receive the
credentials.
+
+The following example sends a bearer token, read from a property, with each
request:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:start")
+
.to("https://api.example.com/orders?authMethod=Bearer&authBearerToken={{orders.token}}&authenticationPreemptive=true");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="direct:start"/>
+ <to
uri="https://api.example.com/orders?authMethod=Bearer&authBearerToken={{orders.token}}&authenticationPreemptive=true"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: direct:start
+ steps:
+ - to:
+ uri: "https://api.example.com/orders"
+ parameters:
+ authMethod: Bearer
+ authBearerToken: "{{orders.token}}"
+ authenticationPreemptive: true
+----
+====
+
+To get the token from an authorization server, use the OAuth2 options
described in the next section.
=== OAuth2 Support
diff --git
a/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/http.json
b/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/http.json
index 5ba15d9f45ba..85ef1dc6fc30 100644
---
a/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/http.json
+++
b/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/http.json
@@ -151,7 +151,7 @@
"proxyPort": { "index": 48, "kind": "parameter", "displayName": "Proxy
Port", "group": "proxy", "label": "producer,proxy", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "description": "Proxy server port" },
"authBearerToken": { "index": 49, "kind": "parameter", "displayName":
"Auth Bearer Token", "group": "security", "label": "producer,security",
"required": false, "type": "string", "javaType": "java.lang.String",
"deprecated": false, "autowired": false, "secret": true, "security": "secret",
"description": "Authentication bearer token" },
"authDomain": { "index": 50, "kind": "parameter", "displayName": "Auth
Domain", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication domain to
use with NTLM" },
- "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
preemptive basic authentication to the server." },
+ "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
the Basic credentials or the Bearer token with the first request, without
waiting for the server to ask for them with a 401 response." },
"authHost": { "index": 52, "kind": "parameter", "displayName": "Auth
Host", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication host to use
with NTLM" },
"authMethod": { "index": 53, "kind": "parameter", "displayName": "Auth
Method", "group": "security", "label": "producer,security", "required": false,
"type": "enum", "javaType": "java.lang.String", "enum": [ "Basic", "Bearer",
"NTLM" ], "deprecated": false, "autowired": false, "secret": false,
"description": "The authentication method to use: Basic, Bearer or NTLM. (NTLM
is deprecated)" },
"authPassword": { "index": 54, "kind": "parameter", "displayName": "Auth
Password", "group": "security", "label": "producer,security", "required":
false, "type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": true, "security": "secret", "description":
"Authentication password" },
diff --git
a/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/https.json
b/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/https.json
index 9280f47dd985..56a3697f31d9 100644
---
a/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/https.json
+++
b/components/camel-http/src/generated/resources/META-INF/org/apache/camel/component/http/https.json
@@ -151,7 +151,7 @@
"proxyPort": { "index": 48, "kind": "parameter", "displayName": "Proxy
Port", "group": "proxy", "label": "producer,proxy", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "description": "Proxy server port" },
"authBearerToken": { "index": 49, "kind": "parameter", "displayName":
"Auth Bearer Token", "group": "security", "label": "producer,security",
"required": false, "type": "string", "javaType": "java.lang.String",
"deprecated": false, "autowired": false, "secret": true, "security": "secret",
"description": "Authentication bearer token" },
"authDomain": { "index": 50, "kind": "parameter", "displayName": "Auth
Domain", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication domain to
use with NTLM" },
- "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
preemptive basic authentication to the server." },
+ "authenticationPreemptive": { "index": 51, "kind": "parameter",
"displayName": "Authentication Preemptive", "group": "security", "label":
"producer,security", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "If this option is true, camel-http sends
the Basic credentials or the Bearer token with the first request, without
waiting for the server to ask for them with a 401 response." },
"authHost": { "index": 52, "kind": "parameter", "displayName": "Auth
Host", "group": "security", "label": "producer,security", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": true,
"autowired": false, "secret": false, "description": "Authentication host to use
with NTLM" },
"authMethod": { "index": 53, "kind": "parameter", "displayName": "Auth
Method", "group": "security", "label": "producer,security", "required": false,
"type": "enum", "javaType": "java.lang.String", "enum": [ "Basic", "Bearer",
"NTLM" ], "deprecated": false, "autowired": false, "secret": false,
"description": "The authentication method to use: Basic, Bearer or NTLM. (NTLM
is deprecated)" },
"authPassword": { "index": 54, "kind": "parameter", "displayName": "Auth
Password", "group": "security", "label": "producer,security", "required":
false, "type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": true, "security": "secret", "description":
"Authentication password" },
diff --git a/components/camel-http/src/main/docs/http-component.adoc
b/components/camel-http/src/main/docs/http-component.adoc
index 0b58a0d04d6a..a93916cd243d 100644
--- a/components/camel-http/src/main/docs/http-component.adoc
+++ b/components/camel-http/src/main/docs/http-component.adoc
@@ -740,10 +740,73 @@ Most of the option are available here:
https://hc.apache.org/httpcomponents-clie
To disable cookies in the CookieStore, you can set the HTTP Client to ignore
cookies by
adding this URI option: `httpClient.cookieSpec=ignore`. This doesn't affect
cookies manually set in the `Cookie` header
-=== Basic auth with the streaming message body
+=== Authentication
-To avoid the `NonRepeatableRequestException`, you need to do the
-Preemptive Basic Authentication by adding the option:
`authenticationPreemptive=true`
+The HTTP producer authenticates with the options of the endpoint. To use the
same settings for all the endpoints of a
+component, set them in an `HttpConfiguration` given to the `httpConfiguration`
option of the component.
+
+[cols="1,3"]
+|===
+| Method | Options
+
+| Basic
+| `authUsername` and `authPassword`. `authMethod=Basic` is optional.
+
+| Bearer
+| `authMethod=Bearer` and `authBearerToken`.
+
+| NTLM (deprecated)
+| `authMethod=NTLM`, `authUsername`, `authPassword`, and `authDomain` if the
account is in a domain.
+|===
+
+By default, the credentials are sent only when the server answers with `401
Unauthorized` and a `WWW-Authenticate`
+header that asks for them. With `authenticationPreemptive=true`, the Basic
credentials or the Bearer token are sent
+with the first request. Use it when the server does not send a challenge, or
when the message body is a stream that
+can be sent only once: otherwise the request fails with
`NonRepeatableRequestException` when the server asks for the
+credentials.
+
+When `followRedirects=true`, a redirect to another host does not receive the
credentials.
+
+The following example sends a bearer token, read from a property, with each
request:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:start")
+
.to("https://api.example.com/orders?authMethod=Bearer&authBearerToken={{orders.token}}&authenticationPreemptive=true");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="direct:start"/>
+ <to
uri="https://api.example.com/orders?authMethod=Bearer&authBearerToken={{orders.token}}&authenticationPreemptive=true"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: direct:start
+ steps:
+ - to:
+ uri: "https://api.example.com/orders"
+ parameters:
+ authMethod: Bearer
+ authBearerToken: "{{orders.token}}"
+ authenticationPreemptive: true
+----
+====
+
+To get the token from an authorization server, use the OAuth2 options
described in the next section.
=== OAuth2 Support
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultAuthenticationHttpClientConfigurer.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultAuthenticationHttpClientConfigurer.java
index 13a1a9b4f160..4b3166615093 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultAuthenticationHttpClientConfigurer.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultAuthenticationHttpClientConfigurer.java
@@ -41,15 +41,19 @@ public class DefaultAuthenticationHttpClientConfigurer
implements HttpClientConf
private final String host;
private final Integer port;
private final String bearerToken;
+ private final boolean ntlm;
private final HttpCredentialsHelper credentialsHelper;
public DefaultAuthenticationHttpClientConfigurer(String user, String pwd,
String domain, String host, String bearerToken,
HttpCredentialsHelper
credentialsHelper) {
- this(user, pwd, domain, null, host, null, bearerToken,
credentialsHelper);
+ this(user, pwd, domain, null, host, null, bearerToken, false,
credentialsHelper);
}
+ /**
+ * @param ntlm whether to authenticate with NTLM (authMethod=NTLM); a
domain also selects NTLM
+ */
DefaultAuthenticationHttpClientConfigurer(String user, String pwd, String
domain, String scheme, String host,
- Integer port, String bearerToken,
+ Integer port, String
bearerToken, boolean ntlm,
HttpCredentialsHelper
credentialsHelper) {
this.username = user;
this.password = pwd == null ? new char[0] : pwd.toCharArray();
@@ -58,13 +62,14 @@ public class DefaultAuthenticationHttpClientConfigurer
implements HttpClientConf
this.host = host;
this.port = port;
this.bearerToken = bearerToken;
+ this.ntlm = ntlm;
this.credentialsHelper = credentialsHelper;
}
@Override
public void configureHttpClient(HttpClientBuilder clientBuilder) {
Credentials defaultcreds;
- if (domain != null) {
+ if (ntlm || domain != null) {
defaultcreds = new NTCredentials(username, password, host, domain);
// NTLM is not included by default so we need to rebuild the
registry to include NTLM
var autoSchemes = RegistryBuilder.<AuthSchemeFactory> create()
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
index 534a21c3481a..3ff94313e80a 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
@@ -338,21 +338,30 @@ public class HttpComponent extends HttpCommonComponent
implements RestProducerFa
private HttpClientConfigurer configureBasicAuthentication(
Map<String, Object> parameters, HttpClientConfigurer configurer,
HttpCredentialsHelper credentialsProvider, URI targetUri) {
+ // not removed from the parameters, so the endpoint gets the options
too
+ String authMethod = getParameter(parameters, "authMethod",
String.class);
String authUsername = getParameter(parameters, "authUsername",
String.class);
String authPassword = getParameter(parameters, "authPassword",
String.class);
+ String authBearerToken = getParameter(parameters, "authBearerToken",
String.class);
+ String authHost = getParameter(parameters, "authHost", String.class);
- if (authUsername != null && authPassword != null) {
+ if ("bearer".equalsIgnoreCase(authMethod) && authBearerToken != null) {
+ return CompositeHttpConfigurer.combineConfigurers(configurer,
+ new DefaultAuthenticationHttpClientConfigurer(
+ null, null, null, authScopeScheme(authHost,
targetUri),
+ authScopeHost(authHost, targetUri),
authScopePort(authHost, targetUri), authBearerToken,
+ false, credentialsProvider));
+ } else if (authUsername != null && authPassword != null) {
String authDomain = getParameter(parameters, "authDomain",
String.class);
- String authHost = getParameter(parameters, "authHost",
String.class);
return CompositeHttpConfigurer.combineConfigurers(configurer,
new DefaultAuthenticationHttpClientConfigurer(
authUsername, authPassword, authDomain,
authScopeScheme(authHost, targetUri),
authScopeHost(authHost, targetUri),
authScopePort(authHost, targetUri), null,
- credentialsProvider));
+ "ntlm".equalsIgnoreCase(authMethod),
credentialsProvider));
} else if (this.httpConfiguration != null) {
- if
("basic".equalsIgnoreCase(this.httpConfiguration.getAuthMethod())
- ||
"bearer".equalsIgnoreCase(this.httpConfiguration.getAuthMethod())) {
+ String method = this.httpConfiguration.getAuthMethod();
+ if ("basic".equalsIgnoreCase(method) ||
"bearer".equalsIgnoreCase(method) || "ntlm".equalsIgnoreCase(method)) {
return CompositeHttpConfigurer.combineConfigurers(configurer,
new DefaultAuthenticationHttpClientConfigurer(
this.httpConfiguration.getAuthUsername(),
@@ -361,7 +370,7 @@ public class HttpComponent extends HttpCommonComponent
implements RestProducerFa
authScopeHost(this.httpConfiguration.getAuthHost(), targetUri),
authScopePort(this.httpConfiguration.getAuthHost(), targetUri),
this.httpConfiguration.getAuthBearerToken(),
- credentialsProvider));
+ "ntlm".equalsIgnoreCase(method),
credentialsProvider));
}
}
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
index a4655a50417b..5846114cada9 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
@@ -150,7 +150,8 @@ public class HttpEndpoint extends HttpCommonEndpoint
implements LineNumberAware
+ " If the component has disabled cookie
management then this option is disabled too.")
private boolean clearExpiredCookies = true;
@UriParam(label = "producer,security",
- description = "If this option is true, camel-http sends
preemptive basic authentication to the server.")
+ description = "If this option is true, camel-http sends the
Basic credentials or the Bearer token with the first"
+ + " request, without waiting for the server to ask
for them with a 401 response.")
private boolean authenticationPreemptive;
@UriParam(label = "producer,advanced", description = "Whether the HTTP GET
should include the message body or not."
+ " By default HTTP
GET do not include any HTTP body. However in some rare cases users may need to
be able to include the message body.")
@@ -547,7 +548,8 @@ public class HttpEndpoint extends HttpCommonEndpoint
implements LineNumberAware
}
/**
- * If this option is true, camel-http sends preemptive basic
authentication to the server.
+ * If this option is true, camel-http sends the Basic credentials or the
Bearer token with the first request,
+ * without waiting for the server to ask for them with a 401 response.
*/
public void setAuthenticationPreemptive(boolean authenticationPreemptive) {
this.authenticationPreemptive = authenticationPreemptive;
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
index 0c963b72b6aa..fdde154f855a 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
@@ -19,10 +19,14 @@ package org.apache.camel.component.http;
import java.io.IOException;
import org.apache.hc.client5.http.auth.AuthCache;
+import org.apache.hc.client5.http.auth.AuthChallenge;
import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.BearerToken;
+import org.apache.hc.client5.http.auth.ChallengeType;
import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.CredentialsProvider;
+import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.classic.ExecChain;
import org.apache.hc.client5.http.classic.ExecChainHandler;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
@@ -69,7 +73,7 @@ public class PreemptiveAuthExecChainHandler implements
ExecChainHandler {
if ("NTLM".equalsIgnoreCase(endpoint.getAuthMethod())) {
authScheme = new NTLMScheme();
} else if ("BEARER".equalsIgnoreCase(endpoint.getAuthMethod())) {
- authScheme = new BearerScheme();
+ authScheme = preemptiveBearerScheme(httpHost, credentials,
context);
} else {
BasicScheme bs = new BasicScheme();
bs.initPreemptive(credentials);
@@ -81,4 +85,19 @@ public class PreemptiveAuthExecChainHandler implements
ExecChainHandler {
}
return chain.proceed(request, scope);
}
+
+ /**
+ * The auth cache keeps the state of a scheme only once its challenge is
complete, and a bearer scheme takes its
+ * token in {@link BearerScheme#isResponseReady}, so a new scheme would be
cached without the token.
+ */
+ private static AuthScheme preemptiveBearerScheme(HttpHost host,
Credentials credentials, HttpClientContext context)
+ throws HttpException {
+ if (!(credentials instanceof BearerToken)) {
+ throw new HttpException("No bearer token for preemptive
authentication");
+ }
+ BearerScheme scheme = new BearerScheme();
+ scheme.processChallenge(new AuthChallenge(ChallengeType.TARGET,
StandardAuthScheme.BEARER), context);
+ scheme.isResponseReady(host, (authScope, ctx) -> credentials, context);
+ return scheme;
+ }
}
diff --git
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBearerAndNtlmAuthenticationTest.java
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBearerAndNtlmAuthenticationTest.java
new file mode 100644
index 000000000000..001a64e4f489
--- /dev/null
+++
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBearerAndNtlmAuthenticationTest.java
@@ -0,0 +1,239 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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 org.apache.camel.component.http;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.http.common.HttpConfiguration;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The endpoint options {@code authMethod=Bearer} with {@code
authBearerToken}, and {@code authMethod=NTLM} without
+ * {@code authDomain}, must authenticate the same way as the same settings on
the component.
+ */
+public class HttpBearerAndNtlmAuthenticationTest extends BaseHttpTest {
+
+ private static final String TOKEN = "abc";
+
+ private final List<String> seenByServer = new CopyOnWriteArrayList<>();
+ private final List<String> seenByOtherHost = new CopyOnWriteArrayList<>();
+
+ private HttpServer localServer;
+ private HttpServer otherHost;
+
+ @Override
+ public void setupResources() throws Exception {
+ otherHost = ServerBootstrap.bootstrap()
+ .setCanonicalHostName("127.0.0.1")
+ .register("/secure", bearerProtected(seenByOtherHost))
+ .create();
+ otherHost.start();
+
+ localServer = ServerBootstrap.bootstrap()
+ .setCanonicalHostName("localhost")
+ .register("/secure", bearerProtected(seenByServer))
+ .register("/basic", (request, response, context) -> {
+ Header auth =
request.getFirstHeader(HttpHeaders.AUTHORIZATION);
+ seenByServer.add(auth == null ? "<none>" :
auth.getValue());
+ if (auth != null) {
+ response.setCode(HttpStatus.SC_OK);
+ response.setEntity(new StringEntity(auth.getValue()));
+ } else {
+ response.setCode(HttpStatus.SC_UNAUTHORIZED);
+ response.setHeader(HttpHeaders.WWW_AUTHENTICATE,
"Basic realm=\"camel\"");
+ }
+ })
+ .register("/ntlm", (request, response, context) -> {
+ Header auth =
request.getFirstHeader(HttpHeaders.AUTHORIZATION);
+ seenByServer.add(auth == null ? "<none>" :
auth.getValue());
+ response.setCode(HttpStatus.SC_UNAUTHORIZED);
+ response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "NTLM");
+ })
+ .register("/redirect", (request, response, context) -> {
+ Header auth =
request.getFirstHeader(HttpHeaders.AUTHORIZATION);
+ seenByServer.add(auth == null ? "<none>" :
auth.getValue());
+ response.setCode(HttpStatus.SC_MOVED_TEMPORARILY);
+ response.setHeader(HttpHeaders.LOCATION,
"http://127.0.0.1:" + otherHost.getLocalPort() + "/secure");
+ })
+ .create();
+ localServer.start();
+ }
+
+ private static HttpRequestHandler bearerProtected(List<String> seen) {
+ return (request, response, context) -> {
+ Header auth = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
+ seen.add(auth == null ? "<none>" : auth.getValue());
+ if (auth != null && ("Bearer " + TOKEN).equals(auth.getValue())) {
+ response.setCode(HttpStatus.SC_OK);
+ response.setEntity(new StringEntity("ok"));
+ } else {
+ response.setCode(HttpStatus.SC_UNAUTHORIZED);
+ response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer
realm=\"camel\"");
+ }
+ };
+ }
+
+ @Override
+ public void cleanupResources() throws Exception {
+ closeImmediately(localServer, otherHost).close();
+ }
+
+ private String url(String path) {
+ return "http://localhost:" + localServer.getLocalPort() + path;
+ }
+
+ private Exchange get(String uri) {
+ return template.request(uri, exchange -> {
+ });
+ }
+
+ @Test
+ public void bearerTokenOnEndpointAnswersTheChallenge() {
+ Exchange out = get(url("/secure?authMethod=Bearer&authBearerToken=" +
TOKEN));
+
+ assertNull(out.getException());
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals("ok", out.getMessage().getBody(String.class));
+ assertEquals(List.of("<none>", "Bearer " + TOKEN), seenByServer);
+ }
+
+ @Test
+ public void bearerTokenOnEndpointIsSentPreemptively() {
+ Exchange out = get(url("/secure?authMethod=Bearer&authBearerToken=" +
TOKEN + "&authenticationPreemptive=true"));
+
+ assertNull(out.getException());
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals(List.of("Bearer " + TOKEN), seenByServer);
+ }
+
+ @Test
+ public void bearerTokenOnComponentAnswersTheChallenge() {
+ context.addComponent("bearer", componentWithBearerToken());
+
+ Exchange out = get("bearer://localhost:" + localServer.getLocalPort()
+ "/secure");
+
+ assertNull(out.getException());
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals(List.of("<none>", "Bearer " + TOKEN), seenByServer);
+ }
+
+ @Test
+ public void bearerTokenOnComponentIsSentPreemptively() {
+ context.addComponent("bearer", componentWithBearerToken());
+
+ Exchange out = get("bearer://localhost:" + localServer.getLocalPort()
+ "/secure?authenticationPreemptive=true");
+
+ assertNull(out.getException());
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals(List.of("Bearer " + TOKEN), seenByServer);
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void bearerTokenIsNotSentToARedirectTargetOnAnotherHost(boolean
preemptive) {
+ Exchange out = get(url("/redirect?authMethod=Bearer&authBearerToken="
+ TOKEN
+ +
"&followRedirects=true&throwExceptionOnFailure=false&authenticationPreemptive="
+ + preemptive));
+
+ assertEquals(401,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertTrue(seenByOtherHost.stream().noneMatch(h -> h.contains(TOKEN)),
"other host received: " + seenByOtherHost);
+ }
+
+ @Test
+ public void authorizationHeaderIsSentAsIs() {
+ Exchange out = template.request(url("/secure"),
+ exchange ->
exchange.getMessage().setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + TOKEN));
+
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals(List.of("Bearer " + TOKEN), seenByServer);
+ }
+
+ @Test
+ public void basicCredentialsOnEndpointAreSentPreemptively() {
+ Exchange out =
get(url("/basic?authMethod=Basic&authUsername=u&authPassword=p&authenticationPreemptive=true"));
+
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals(List.of("Basic dTpw"), seenByServer);
+ }
+
+ @Test
+ public void basicCredentialsOnEndpointAnswerTheChallenge() {
+ Exchange out = get(url("/basic?authUsername=u&authPassword=p"));
+
+ assertEquals(200,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals(List.of("<none>", "Basic dTpw"), seenByServer);
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void ntlmOnEndpointWithoutDomainAnswersTheChallenge(boolean
preemptive) {
+ Exchange out =
get(url("/ntlm?authMethod=NTLM&authUsername=u&authPassword=p&throwExceptionOnFailure=false"
+ + "&authenticationPreemptive=" + preemptive));
+
+ assertEquals(401,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertTrue(seenByServer.stream().anyMatch(h -> h.startsWith("NTLM ")),
"server received: " + seenByServer);
+ }
+
+ @Test
+ public void ntlmOnComponentAnswersTheChallenge() {
+ HttpConfiguration configuration = new HttpConfiguration();
+ configuration.setAuthMethod("NTLM");
+ configuration.setAuthUsername("u");
+ configuration.setAuthPassword("p");
+ HttpComponent component = new HttpComponent();
+ component.setHttpConfiguration(configuration);
+ context.addComponent("ntlm", component);
+
+ Exchange out = get("ntlm://localhost:" + localServer.getLocalPort() +
"/ntlm?throwExceptionOnFailure=false");
+
+ assertEquals(401,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertTrue(seenByServer.stream().anyMatch(h -> h.startsWith("NTLM ")),
"server received: " + seenByServer);
+ }
+
+ @Test
+ public void ntlmOnEndpointWithDomainAnswersTheChallenge() {
+ Exchange out =
get(url("/ntlm?authMethod=NTLM&authUsername=u&authPassword=p&authDomain=D"
+ + "&throwExceptionOnFailure=false"));
+
+ assertEquals(401,
out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertTrue(seenByServer.stream().anyMatch(h -> h.startsWith("NTLM ")),
"server received: " + seenByServer);
+ }
+
+ private static HttpComponent componentWithBearerToken() {
+ HttpConfiguration configuration = new HttpConfiguration();
+ configuration.setAuthMethod("Bearer");
+ configuration.setAuthBearerToken(TOKEN);
+ HttpComponent component = new HttpComponent();
+ component.setHttpConfiguration(configuration);
+ return component;
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 3e9467ea04d0..08a621a0ae8d 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -553,6 +553,16 @@ read. A custom `httpBinding` set on the component is now
used by the Rest DSL co
`ServletRestHttpBinding`. Any other binding set on the component is still used
only by `servlet:` routes, as it does
not map the path placeholders such as `+{id}+` to headers.
+=== camel-http - authMethod=Bearer and authMethod=NTLM are applied
+
+The endpoint options `authMethod=Bearer` and `authBearerToken` now send the
token when the server asks for it. Before,
+only a token set in the `httpConfiguration` of the component was sent. With
`authenticationPreemptive=true`, the token
+is now sent with the first request; before, the request failed with
`IllegalStateException: Bearer token is null`.
+
+`authMethod=NTLM`, on the endpoint or in the `httpConfiguration` of the
component, now authenticates with NTLM also
+when `authDomain` is not set. Before, the endpoint answered only Basic and
Digest challenges with these credentials,
+and the component did not use them. If the server asks for Basic credentials,
remove `authMethod=NTLM`.
+
=== Java
*Breaking change:* Camel 4.24 requires *Java 21* as the minimum runtime
version.
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
index e24188870f04..a5b6c922cd04 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
@@ -648,8 +648,9 @@ public interface HttpEndpointBuilderFactory {
return this;
}
/**
- * If this option is true, camel-http sends preemptive basic
- * authentication to the server.
+ * If this option is true, camel-http sends the Basic credentials or
the
+ * Bearer token with the first request, without waiting for the server
+ * to ask for them with a 401 response.
*
* The option is a: <code>boolean</code> type.
*
@@ -664,8 +665,9 @@ public interface HttpEndpointBuilderFactory {
return this;
}
/**
- * If this option is true, camel-http sends preemptive basic
- * authentication to the server.
+ * If this option is true, camel-http sends the Basic credentials or
the
+ * Bearer token with the first request, without waiting for the server
+ * to ask for them with a 401 response.
*
* The option will be converted to a <code>boolean</code> type.
*