1

我尝试使用Apache HttpClient 5执行 http 请求,并且我需要 NTLM 身份验证,该客户端开箱即用地支持该身份验证。但是当我尝试通过 HttpComponentsClientHttpConnector 将它与 Spring WebClient 一起使用时,出现了一个问题。

HttpComponentsClientHttpConnector初始化HttpComponentsClientHttpRequest以执行请求。但是如果请求有正文,HttpComponentsClientHttpRequest会初始化 ReactiveEntityProducer,这是不可重复的,因此,虽然我需要执行 3 个请求以使用 NTLM 进行身份验证,但实际上只发生了一个。

是否有可能改变这种行为?

UPD

添加了我的原型 java 代码:

BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
    new AuthScope("https", "some-host", 443, null, StandardAuthScheme.NTLM),
    new NTCredentials("test-user", "test-password".toCharArray(), null, null)
);

WebClient webClient = WebClient.builder()
    .clientConnector(new HttpComponentsClientHttpConnector
        (HttpAsyncClients
            .custom()
            .setDefaultCredentialsProvider(credsProvider)
            .setTargetAuthenticationStrategy(DefaultAuthenticationStrategy.INSTANCE)
            .setDefaultRequestConfig(
                RequestConfig.custom()
                    .setAuthenticationEnabled(true)
                    .setTargetPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.NTLM))
                    .setExpectContinueEnabled(true)
                    .build())
            .build()))
    .build();

webClient
    .method(HttpMethod.POST)
    .uri("https://some-host/execute")
    .header("Content-Type", "text/xml; charset=utf-8")
    .header("SOAPAction", "http://schemas.microsoft.com/exchange/services/2006/messagesCreateItem")
    .header("Accept-Encoding", "gzip, deflate, br")
    .header("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7")
    .header("Expect", "100-continue")
    .body(BodyInserters.fromValue(
        "<SOME_XML_BODY>"
    ))
    .exchangeToMono(clientResponse -> {
        return clientResponse.bodyToMono(Object.class);
    })
    .subscribe();
4

0 回答 0