我有以下代码向服务器发出 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/