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

时间:2019-02-06 13:47:10

标签: java ntlm waffle

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

该Web部件提供REST服务,并使用waffle.servlet.NegotiateSecurityFilter过滤对服务的请求,该HttpURLConnection提取Windows登录信息以识别用户。

客户端部分使用"dev-ca": "npm run development-ca", "development-ca": "cross-env process.env.location=us NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" 将请求发送到Web部件。据我了解,Ntlm信息会自动打包到请求中。

虽然我在Eclipse中进行测试,但效果很好。当我部署客户端JAR时,它不起作用。我收到未认证的401。

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

我安装了JRE“ 1.8.0_201”和JDK“ 1.8.0_161”。

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

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

2 个答案:

答案 0 :(得分:2)

我没有找到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);
            }
        }

答案 1 :(得分:2)

我认为How to provide ntlm authentication while calling any url?的第一个答案可以回答这个问题。 对于Java 8u201,有一个新的JRE选项 jdk.http.ntlm.transparentAuth 缺省情况下被设置为禁用