java-HttpClient不通过NTLM进行身份验证

时间:2018-09-26 07:03:22

标签: java windows-authentication apache-commons-httpclient

尝试连接到受NTLM身份验证保护的资源。 发出请求时,我得到未经身份验证的响应401,但此后httpclient不会执行NTLM身份验证。

添加了拦截器以查看通信,它甚至没有尝试进行身份验证:

Request:
POST/NAV/xxxxxxxxx
Content-type: text/xml; charset=utf-8
SOAPAction:
Content-Length: 359
Host: xxx.local:7051
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.4 (Java/1.8.0_181)
Accept-Encoding: gzip,deflate


Response:
Unauthorized
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
WWW-Authenticate: Negotiate
Date: Wed, 26 Sep 2018 10:37:56 GMT

此后没有任何请求。

任何建议在这里有什么问题吗?

这是我的代码:

NTCredentials credentials = new NTCredentials("testuser", "pass1", null, "stt.local");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, credentials);

        ArrayList<String> authPrefs = new ArrayList<String>();
        authPrefs.add(AuthSchemes.NTLM);


        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(30000)
                .setConnectTimeout(30000)
                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
                .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
                .build();


        HttpClient client = HttpClientBuilder.
                create().
                setDefaultCredentialsProvider(credsProvider).
                setDefaultRequestConfig(requestConfig).
                addInterceptorLast(new LoggingRequestInterceptor()).
                addInterceptorLast(new LoggingResponseInterceptor()).
                build();



        HttpPost post = new HttpPost(endpoint); //Provide Request URL


        try {

            StringEntity input = new StringEntity(bodyAsString);
            input.setContentType("text/xml; charset=utf-8");
            post.setEntity(input);

            post.setHeader("Content-type", "text/xml; charset=utf-8");
            post.setHeader("SOAPAction", ""); //Provide Soap action


            org.apache.http.HttpResponse response = client.execute(post);
    }

1 个答案:

答案 0 :(得分:1)

NTCredentials构造函数的参数应具有单独的用户名和域名。

  

参数:
  userName-用户名。这不应包括要进行身份验证的域。例如:“用户”正确,而“域\用户”正确。
  密码-密码。
  工作站-身份验证请求所源自的工作站。    本质上,这是该计算机的计算机名称。
  域-要在其中进行身份验证的域。