HTTP 请求在不同的环境中获得授权和未授权,因为它看起来是相同的设置

HTTP request gets authorized and unauthorized on different environments with as it seems same setup

我们有一个有趣的情况,基本的 GET HTTP 请求没有通过 IIS 服务器上的 Windows NTLM 授权。同时,我们在另一个成功执行的环境中有相同的代码 运行。

当我们通过浏览器重复请求时,它会成功执行。

似乎 Java 代码没有随请求发送正确的授权信息。我们正试图弄清楚这是怎么回事。 类 使用的是 java.net 包。 我们尝试将帐户切换到 Tomcat 是 运行 的本地系统,但没有成功。

代码尽可能简单:

public static String sendHttpRequest(String urlString, String method, boolean 
disconnect) {

    HttpURLConnection urlConnection = null;
    String result = null;
    try {

        URL url = new URL(urlString);

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.addRequestProperty("Content-Length", "0");

        urlConnection.setRequestMethod(method);
        urlConnection.setUseCaches(false);

         StringBuilder sb = new StringBuilder();
        try (InputStream is = urlConnection.getInputStream()) {

             InputStream buffer = new BufferedInputStream(is);
            Reader reader = new InputStreamReader(buffer, "UTF-8");
            int c;

            while ((c = reader.read()) != -1) {
                sb.append((char) c);
            }
        }

        int statusCode = urlConnection.getResponseCode();
        if (statusCode < 200 || statusCode >= 300) {

        } else
            result = sb.toString();

    } catch (IOException e) {
        LOGGER.warning(e.getMessage());
    } finally {
        if (disconnect && urlConnection != null) {
            urlConnection.disconnect();
        }
    }

    return result;
}

要回答的明确问题: 如何在客户端使用log/trace/debug信息进行认证?任何提示将不胜感激:)

据我所知,vanilla JDK 本身没有 built-in NTLM 支持。这意味着您必须自己手动连接协议的步骤。

我是根据我的经验写这篇文章的:我必须自己滚动整个 multi-round SPNEGO,它支持 Oracle IBM JRE。很(不)有趣。虽然,这只是 server-side 的乐趣,但我记得在 Java-client 方面也缺少这个功能(因为它是 SPNEGO 而不是普通的 NTLM,客户端是浏览器,所以我可以跳过那部分)。

Java11 中的新 HTTP 客户端可能已经改变了,我还没有这方面的经验。

为了调试您的身份验证交换,您可以添加命令行参数:

-Djavax.net.debug=ssl,handshake.

这样,您将在握手过程中逐步为两个客户端。

Apache HttpClient 的较新版本支持本机 Windows 当 运行 在 Windows OS 上通过 JNA 通过 SSPI 协商、Kerberos 和 NTLM。因此,如果您可以选择使用较新的版本(我相信是从 4.​​4 开始),这不是问题。

例如: http://hc.apache.org/httpcomponents-client-4.4.x/httpclient-win/examples/org/apache/http/examples/client/win/ClientWinAuth.java