[英]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.