【问题标题】:Client resource post fails for NTLM authentication. Works for Basic Authentication in apacheNTLM 身份验证的客户端资源发布失败。适用于 apache 中的基本身份验证
【发布时间】:2015-10-07 06:10:47
【问题描述】:

我有以下代码向服务器发出 POST 请求。但是,Web 服务器可以是 Apache 或 IIS。

Client client = new Client(new Context(), Protocol.HTTP);   
ClientResource resource = new ClientResource(url);
resource.setRetryOnError(false);
resource.setNext(client);

resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,userName,pwd);

response = resource.post(representation);

以下代码适用于 apache,但对于 IIS 失败并出现以下错误:

WARNING: Couldn't find any helper support the HTTP_NTLM challenge scheme.
WARNING: Couldn't find any helper support the HTTP_Negotiate challenge scheme.
Exception in thread "main" Unauthorized (401) - The request requires user authentication

可能原因是 apache 使用基本身份验证,而 IIS 使用 NTML。第一次尝试显然是将 IIS 的挑战方案更改为 NTLM,如下所示,但得到了相同的错误(我也已经为 restlet jar 添加了网络扩展)。

 resource.setChallengeResponse(ChallengeScheme.HTTP_NTLM, userName, pwd);

另外,我认为有一种方法可以使用 Apache http 客户端 (NTCredentials) 类,但我仍然想使用 restlet jar 来避免对现有代码进行大量更改。

有什么建议吗?任何帮助,将不胜感激。 提前致谢。

【问题讨论】:

    标签: java apache rest iis restlet


    【解决方案1】:

    Restlet 中没有对 NTLM 的内置支持。 Restlet Github 存储库中的一个问题解决了这样的问题:https://github.com/restlet/restlet-framework-java/issues/467

    我还在 Restlet 文档中看到了一个关于 NTLM 的页面:http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/ntml-authentication。但它似乎有点过时,特别是对于 HTTPClient 部分。此外,不推荐使用 HTTP 客户端扩展并将在版本 3 中删除。应使用 Jetty 扩展。

    也就是说,您可以尝试使用 Java 本身提供的 NTLM 支持。为此,您需要使用 Restlet 的默认 HTTP 客户端,即在类路径中未提供客户端连接器时使用的客户端。这是一个使用示例:

    final String username = "username";
    final String password = "password";
    // Create your own authenticator
    Authenticator a = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication(
                      username, password.toCharArray()));
        }
    };
    // Sets the default Authenticator
    Authenticator.setDefault(a);
    
    ClientResource cr = new ClientResource("http://...");
    cr.post(...);
    

    此链接可以帮助您做到这一点:http://examples.javacodegeeks.com/core-java/net/authenticator/access-password-protected-url-with-authenticator/

    希望对你有帮助 蒂埃里

    【讨论】:

    • 最终更改了 IIS 本身的身份验证。启用基本身份验证(是的,我知道它不够安全),因此 ChallengeScheme.HTTP_BASIC 的相同代码适用于 Apache 和 IIS。
    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 2018-07-03
    • 2022-06-23
    • 2022-10-13
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多