HttpURLConnection中的NTLM身份验证在JRE中不起作用,但在JDK环境中工作



我正在使用 eclipse 来开发应用程序的 2 个部分。

Web 部件提供 REST 服务,并且使用提取 Windows 登录信息以标识用户的waffle.servlet.NegotiateSecurityFilter筛选对服务的请求。

客户端部件使用 HttpURLConnection 向 Web 部件发送请求。据我了解,Ntlm 信息会自动打包到请求中。

当我在日食中测试它时,它工作正常。当我部署客户端 JAR 时,它不起作用。我收到 401 未通过身份验证。

经过一些调查,我发现我可以通过将执行环境设置为 JRE 而不是默认的 JDK 来在 eclipe 中重现这一点。

我安装了JRE"1.8.0_

201"和JDK"1.8.0_161"。

因此,只需将执行环境从 JRE 更改为 JDK,我就可以获得连接进行身份验证。

JDK 有什么不同之处,我该怎么做才能让客户端使用 JRE?

我认为

第一个答案 如何在调用任何网址时提供 ntlm 身份验证? 可以回答这个问题。在Java 8u201中,有一个新的JRE选项jdk.http.ntlm.transparentAuth默认设置为禁用

我没有设法找到 JRE 和 JDK 之间的区别。相反,我找到了这个解决方法。

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-win -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient-win</artifactId>
    <version>4.5.7</version>
</dependency>

示例代码

        if (!WinHttpClients.isWinAuthAvailable()) {
            log.warn("Integrated Win auth is not supported!!!");
        }
        // There is no need to provide user credentials
        // HttpClient will attempt to access current user security context through
        // Windows platform specific methods via JNI.
        try (CloseableHttpClient httpclient = WinHttpClients.createDefault()) {
            HttpGet httpget = new HttpGet(getRestUrl().toURI());
            log.debug("Executing request " + httpget.getRequestLine());
            try (CloseableHttpResponse response = httpclient.execute(httpget)) {
                int status = response   .getStatusLine()
                                        .getStatusCode();
                if (status != 200) {
                    log.error("HTTP error " + status);
                    throw new RuntimeException("Failed : HTTP error code : " + status);
                }
                Type listType = new TypeToken<HashMap<String, App>>() {
                }.getType();
                return new Gson().fromJson(new InputStreamReader(response   .getEntity()
                                                                            .getContent(),
                        "UTF-8"), listType);
            }
        }

相关内容

最新更新