Java NTLM身份验证的客户端资源post失败。适用于apache中的基本身份验证
我有以下代码向服务器发出POST请求。但是,web服务器可以是Apache或IISJava NTLM身份验证的客户端资源post失败。适用于apache中的基本身份验证,java,apache,rest,iis,restlet,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(Challe
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添加了net扩展)
另外,我认为有一种方法可以使用ApacheHTTP客户机(NTCredentials)类,但我仍然希望使用restlet JAR来避免对现有代码进行大量更改
有什么建议吗?任何帮助都将不胜感激。
提前感谢。Restlet中没有内置的对NTLM的支持。Restlet Github存储库中的一个问题解决了这样的问题: 我还在Restlet文档中看到了一个关于NTLM的页面:。但它似乎有点过时,尤其是对于HTTPClient部分。此外,HTTP客户端扩展已弃用,将在版本3中删除。应改为使用码头扩建部分 也就是说,您可以尝试使用Java本身提供的NTLM支持。为此,您需要使用Restlet的默认HTTP客户机,即在类路径中没有提供客户机连接器时使用的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(...);
此链接可以帮助您做到这一点:
希望它能帮助你,
Thierry最终改变了IIS本身的身份验证。启用了基本身份验证(是的,我知道它不够安全),因此ChallengeScheme.HTTP_Basic的相同代码适用于Apache和IIS。
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(...);