【问题标题】:How to provide ntlm authentication while calling any url?如何在调用任何 url 时提供 ntlm 身份验证?
【发布时间】:2019-02-07 07:21:00
【问题描述】:

我有一个使用 ntlm(Windows 集成身份验证)进行身份验证的托管 URL。我在 Windows 上并使用 java 1.8

URL url = new URL("someUrl");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// con.setInstanceFollowRedirects(false);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestMethod("GET");
 int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
            // read response
            ...
            in.close();
            }else{
            System.out.println("Error while fetching reponse, recieved response code " + responseCode);
            }

上面的代码一直工作到 java 1.8.0_181 随着后续更新它开始失败,我用 191 和 201 进行了测试。 如果向后移植到 181,该代码仍然有效。 我也尝试使用 Authenticator,但它没有被调用(不知道为什么) 使用 java 的内部日志记录,我可以在日志中看到以下消息 “NegotiateAuthentication: java.io.IOException: 协商支持未启动” 我得到 401

我期待任何机制来帮助 java 自行协商以进行身份​​验证。

【问题讨论】:

    标签: java java-8 windows-authentication ntlm


    【解决方案1】:

    在 Java 发行说明中没有提到任何地方,但 NTLM 身份验证实现发生了变化。 我已经调试了java代码并到达了以下 在 java.home/lib 中有文件 net.properties 现在提到以下

    #
    # Transparent NTLM HTTP authentication mode on Windows. Transparent authentication
    # can be used for the NTLM scheme, where the security credentials based on the
    # currently logged in user's name and password can be obtained directly from the
    # operating system, without prompting the user. This property has three possible
    # values which regulate the behavior as shown below. Other unrecognized values
    # are handled the same as 'disabled'. Note, that NTLM is not considered to be a
    # strongly secure authentication scheme and care should be taken before enabling
    # this mechanism.
    #
    # Transparent authentication never used.
    #jdk.http.ntlm.transparentAuth=disabled
    #
    # Enabled for all hosts.
    #jdk.http.ntlm.transparentAuth=allHosts
    #
    # Enabled for hosts that are trusted in Windows Internet settings
    #jdk.http.ntlm.transparentAuth=trustedHosts
    #
    jdk.http.ntlm.transparentAuth=disabled
    

    在 jdk1.8.0_181 之前有一个默认的 NTLM 身份验证回调,这在 NTLM 身份验证过程中很有用。

    要在 jdk1.8.0_181 之后运行上述代码,您只需为您的 java 进程设置 jdk.http.ntlm.transparentAuth

    或者,您可以设置 JVM 参数,例如 -Djdk.http.ntlm.transparentAuth=allHosts,或设置系统属性,例如 System.setProperty("jdk.http.ntlm.transparentAuth", "allHosts")

    如果您选择 trustedHosts,请确保将 URL 添加到 windows 受信任的站点中。

    您可以在此处看到在静态初始化期间使用的这个新系统属性:sun.net.www.protocol.http.ntlm.NTLMAuthentication

    进一步,你可以看到这里使用的设置:public static boolean NTLMAuthentication.isTrustedSite(URL)

    最后,要以编程方式控制 URL 是否可信,您可以安装回调。见:sun.net.www.protocol.http.ntlm.NTLMAuthenticationCallback

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多