java - NTLM 身份验证的客户端资源发布失败。适用于 apache 中的基本身份验证

标签 java apache rest iis restlet

我有以下代码向服务器发出 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 来避免对现有代码进行大量更改。

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

最佳答案

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/ .

希望对你有帮助 蒂埃里

关于java - NTLM 身份验证的客户端资源发布失败。适用于 apache 中的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32984929/

相关文章:

java - 在Android中,如何使用Java反射调用sun.misc.Unsafe方法?

java - 优化 Spring Boot JPA 查询

java 日期持久化

apache - 如何为 Zabbix 启用 HTTPS

apache - 来自单个目录的多个 .htpasswd 文件的源密码

apache - 如果 index.html 不存在于没有尾部斜杠的目录中,则提供 index.php

java - Jersey (jsr 311)和自定义 ViewProcessor

java - 与 Java 进程之间的通信是否仍在微服务中?

java - 查找数组中的差异

rest - 用于 Restful API 的适配器代理