编程语言
首页 > 编程语言> > java-HttpURLConnection中的NTLM身份验证在JRE中不起作用,但在JDK环境中起作用

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

作者:互联网

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

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

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

当我在日食测试时,它工作正常.当我部署客户端JAR时,它不起作用.我收到未认证的401.

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

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

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

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

解决方法:

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

标签:waffle,java,ntlm
来源: https://codeday.me/bug/20191024/1923309.html