【问题标题】:NTLM authentication in HttpURLConnection not working in JRE but works in JDK environmentHttpURLConnection 中的 NTLM 身份验证在 JRE 中不起作用,但在 JDK 环境中起作用
【发布时间】:2019-02-06 13:47:10
【问题描述】:

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

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

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

当我在 Eclipse 中测试它时,它运行良好。当我部署客户端 JAR 时,它不起作用。我收到 401 Not Authenticated。

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

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

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

JDK 有什么不同,我可以做些什么来让客户端使用 JRE?

【问题讨论】:

标签: java ntlm waffle


【解决方案1】:

我觉得How to provide ntlm authentication while calling any url?的第一个回答可以回复这个问题。 在 Java 8u201 中,有一个新的 JRE 选项 jdk.http.ntlm.transparentAuth 默认设置为禁用

【讨论】:

    【解决方案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);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2011-11-12
      • 2020-02-09
      • 2012-03-16
      相关资源
      最近更新 更多