为了账号安全,请及时绑定邮箱和手机立即绑定

HttpURLConnection 中的 NTLM 身份验证在 JRE 中不起作用

HttpURLConnection 中的 NTLM 身份验证在 JRE 中不起作用

烙印99 2022-06-08 17:04:29
我正在使用 eclipse 开发应用程序的两个部分。Web 部件提供 REST 服务并过滤对服务的请求,使用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?
查看完整描述

2 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

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



查看完整回答
反对 回复 2022-06-08
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

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

            }

        }


查看完整回答
反对 回复 2022-06-08
  • 2 回答
  • 0 关注
  • 156 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号