通过 Apache Camel 调用 NTLMv2 安全端点

Call NTLMv2 secured endpoint via Apache Camel

我正在使用 Apache Camel 2.19,并且在使用开箱即用支持 NTLMv1 的 camel-http 模块调用我的端点时,我能够使用 NTLMv1:

from("activemq:{{queue.feedback}}")
    .to("http://localhost:8888/ntlm/secured?authMethodPriority=NTLM 
                 &authMethod=NTLM&authUsername=Zaphod
                 &authPassword=Beeblebrox&authDomain=Minor
                 &authHost=LightCity")

问题是我不知道如何使用 NTLMv2 发出请求。 official documentation 指出:

Note: camel-http is based on HttpClient v3.x and as such has only limited support for what is known as NTLMv1, the early version of the NTLM protocol. It does not support NTLMv2 at all. camel-http4 has support for NTLMv2.

当我尝试使用 camel-http4 时,它什么也没做:

from("activemq:{{queue.feedback}}")
    .to("http4://localhost:8888/ntlm/secured?authMethodPriority=NTLM 
                 &authMethod=NTLM&authUsername=Zaphod
                 &authPassword=Beeblebrox&authDomain=Minor
                 &authHost=LightCity")

camel-http4 似乎根本不知道 NTLM。我试图调查 camel-http4 repo on GitHub,但除了文档之外我找不到任何与 NTLM 相关的内容。

关于如何在 Camel 2.19 中使用 NTLMv2 的任何想法(其他版本的 Camel 可能也很适合)?

问题出在 camel-http4 组件中。默认情况下,它使用 InputStreamEntity,它不是 HttpEntity 实体的可重复实现,这意味着一旦您读取流 - 它就会关闭,您无法再次读取它。这会导致 MainClientExec 失败:

if (execCount > 1 && !RequestEntityProxy.isRepeatable(request)) {
    throw new NonRepeatableRequestException("Cannot retry request with a non-repeatable request entity.");
}

这似乎是一个错误,因此解决方法是在发送请求之前将 InputStreamEntity 转换为 ByteArrayEntity(可重复):

@Component
public class NtlmProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        HttpEntity httpEntity = exchange.getIn().getBody(HttpEntity.class);

        byte[] bytes = EntityUtils.toByteArray(httpEntity);

        ByteArrayEntity byteArrayEntity = new ByteArrayEntity(bytes, ContentType.get(httpEntity));

        exchange.getOut().setBody(byteArrayEntity);
    }
}