繁体   English   中英

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

[英]Client resource post fails for NTLM authentication. Works for Basic Authentication in apache

我有以下代码向服务器发出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客户端,即在类路径中未提供任何客户端连接器时使用的默认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/

希望对您有帮助,蒂埃里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM