【问题标题】:Java client + NTLM Authentication 401Java 客户端 + NTLM 身份验证 401
【发布时间】:2022-10-13 09:10:46
【问题描述】:

我是java新手,我有一个项目可以通过WebRest API与一个系统交换数据。 所以,正常程序,我需要向系统发送 CURL 或 URL 并获取信息,但首先我需要进行身份验证。在使用此系统的服务器上,使用 NTLM 的 Windows 身份验证设置身份验证。我用 JMeter 测试了 URL 链接,得到了正确的答案。 但是当我尝试从我的应用程序中进行通信时,我总是遇到错误 401。 我检查了很多关于身份验证的帖子,但很多都是旧的并且不起作用。 这是我的代码:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;

    public class Main {
        public static void main(String[] args) throws IOException {

            jcifs.Config.registerSmbURLHandler();
            System.setProperty("http.auth.ntlm.domain", "web104.server.local");
            // System.setProperty("jcifs.smb.client.domain", "domain");
            System.setProperty("jcifs.smb.client.username", "ADMINISTRATOR");
            System.setProperty("jcifs.smb.client.password", "Password");
            System.setProperty("jcifs.netbios.hostname", "web104.server.local");
            System.setProperty("java.protocol.handler.pkgs", "65200");
            URL urlRequest = new URL("http://web104.server.local:65200/public/api/sessions/v1/sessions/actions/login");

            HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();

            StringBuilder response = new StringBuilder();

            try {
                InputStream stream = conn.getInputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(stream));

                String str = "";
                while ((str = in.readLine()) != null) {
                    response.append(str);
                }
                in.close();

                System.out.println(response);
            } catch (IOException err) {
                System.out.println(err);
            } finally {
                Map<String, String> msgResponse = new HashMap<String, String>();

                for (int i = 0; ; i++) {
                    String headerName = conn.getHeaderFieldKey(i);
                    String headerValue = conn.getHeaderField(i);
                    if (headerName == null && headerValue == null) {
                        break;
                    }
                    msgResponse.put(headerName == null ? "Method" : headerName, headerValue);
                }

                System.out.println(msgResponse);
            }

        }
    }

你有什么想法,为什么我总是得到错误 401?

【问题讨论】:

    标签: java url windows-authentication


    【解决方案1】:

    代码 401 表示服务器需要身份验证,身份验证设置无效。确保您拥有jcifs放:

    System.setProperty("java.protocol.handler.pkgs", "jcifs");
    

    或者

    jcifs.Config.registerSmbURLHandler();
    

    根据文档,以上是两种设置方式,您都设置了,请只保留一种。

    另一个想法是在 url 中设置用户名和密码。

    参考:jcifs httpclient

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-03
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多