简体   繁体   English

使用httpclient 4.2.1进行NTLM身份验证

[英]NTLM authentication with httpclient 4.2.1

I need to do a HTTP GET to a URL that needs NTLM authentication. 我需要对需要NTLM身份验证的URL执行HTTP GET。 I can access the URL using Firefox or Chrome on a MacBook Pro. 我可以在MacBook Pro上使用Firefox或Chrome访问该URL。 The browser asks for the username/password combo and it works. 浏览器要求输入用户名/密码组合并且它有效。 I am now trying to do the same from Groovy using HttpClient. 我现在正尝试使用HttpClient从Groovy做同样的事情。 I followed the NTLM support guide , but I always get a 401 Unauthorized back. 我遵循了NTLM支持指南 ,但我总是得到401 Unauthorized返回。 There is also this sentence in the response: 回复中还有这句话:

You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept. 您无权使用您提供的凭据查看此目录或页面,因为您的Web浏览器正在发送Web服务器未配置为接受的WWW-Authenticate标头字段。

I tried all kinds of combinations for the servername and domain (the remote windows pc is not on a domain) in this piece of code, but I always get the same response. 我在这段代码中尝试了servername和domain(远程windows pc不在域上)的各种组合,但我总是得到相同的响应。

httpclient.getCredentialsProvider().setCredentials(
new AuthScope("myserver", -1), 
new NTCredentials("username", "password", "MYSERVER", "MYDOMAIN"));

Anybody had the same problem and managed to solve it? 有人遇到同样的问题并设法解决了吗? Note that this is an external program that uses IIS under the hood, so I don't think I can change any settings there. 请注意,这是一个使用IIS的外部程序,所以我认为我不能在那里更改任何设置。

EDIT: 编辑:

Unlike what I have said, I managed to change the security settings in IIS to accept BASIC authentation, so I don't have the problem anymore. 与我所说的不同,我设法更改IIS中的安全设置以接受BASIC认证,所以我不再有问题了。

EDIT: 编辑:

In my experience with setting up Kerberos or NTLM (both are single sign on), you don't have to enter username/password at all when you are already logged in to your system. 根据我设置Kerberos或NTLM(两者都是单点登录)的经验,当您已经登录到系统时,根本不需要输入用户名/密码。

I am pretty sure that when the browser asked for username/password combo, that's not an NTLM authentication at all. 我很确定当浏览器询问用户名/密码组合时,根本不是NTLM身份验证。 Most likely the server side application has a fallback scheme to HTTP Basic Digest (that why it displayed the username/password combo). 服务器端应用程序很可能具有HTTP Basic Digest的回退方案(这就是它显示用户名/密码组合的原因)。 With NTLM you'll never have to enter your username/password (principal/credentials) at all, as the server will recognize who you are through the negotiation mechanism between your browser, your operating system, server and Active Directory server. 使用NTLM,您将永远不必输入您的用户名/密码(委托人/凭证),因为服务器将通过浏览器,操作系统,服务器和Active Directory服务器之间的协商机制识别您的身份。

If your MacBook Pro is running on OS/X, you also need to add your OS/X to the domain. 如果您的MacBook Pro在OS / X上运行,您还需要将OS / X添加到域中。 Your server also needs to be in the same domain where the client OS/X being added. 您的服务器还需要位于添加客户端OS / X的同一域中。 This may not be a trivial case. 这可能不是一个简单的案例。 Some external tools/driver may be needed. 可能需要一些外部工具/驱动程序。 This one may be a good candidate (but I haven't tried that). 可能是一个很好的候选人(但我没有尝试过)。

NTLM needs both the client to be a member of the same domain as the server, hence both needs to be registered in the Active Directory domain. NTLM需要客户端都是与服务器相同的域的成员,因此两者都需要在Active Directory域中注册。 If your server is not in the domain, than that will be another set of problem. 如果您的服务器不在域中,那将是另一组问题。

In order to get your browser works with NTLM, you need to install plugin (ntlmauth-plugin?). 为了使您的浏览器适用于NTLM,您需要安装插件(ntlmauth-plugin?)。 But I have never try that on MacOS/X yet. 但我还没有在MacOS / X上尝试过。 Even in Windows you still need a plugin in order to run Firefox successfully with NTLM. 即使在Windows中,您仍然需要一个插件才能使用NTLM成功运行Firefox。

HttpClient did not work for me but finally the code below worked. HttpClient不适合我,但最终下面的代码工作。 Reference - http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html 参考 - http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html

For quick reference - 供快速参考 -

public static String getResponse(String url, String userName, String password) throws IOException {
Authenticator.setDefault(new Authenticator() {
  @Override
  public PasswordAuthentication getPasswordAuthentication() {
    System.out.println(getRequestingScheme() + " authentication");
    return new PasswordAuthentication(userName, password.toCharArray());
  }
});

URL urlRequest = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");

StringBuilder response = new StringBuilder();
InputStream stream = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str = "";
while ((str = in.readLine()) != null) {
  response.append(str);
}
in.close();

return response.toString();

} }

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

相关问题 apache httpclient + ntlm身份验证 - apache httpclient + ntlm Authentication Apache HttpClient 4.1.1 NTLM身份验证不是SPNEGO - Apache HttpClient 4.1.1 NTLM authentication not SPNEGO 使用HttpClient 4.2.1为Java Twitter应用程序设置OAuth身份验证 - Setting up OAuth authentication for a Java Twitter application using HttpClient 4.2.1 具有NTLM的Java HTTPClient 4.5,无法获得NTLMv2身份验证 - Java HTTPClient 4.5 with NTLM, can't get NTLMv2 authentication WWW-Authentication / NTLM使用具有当前用户凭据的HttpClient进行协商 - WWW-Authentication / NTLM Negotiate using HttpClient with current user credentials 使用SSL加密和NTLM身份验证的HttpClient失败 - HttpClient using both SSL encryption and NTLM authentication fails 您如何通过 apache 的 commons httpclient 使用 NTLM 身份验证以编程方式对 Web 服务器进行身份验证? - How do you programatically authenticate to a web server using NTLM Authentication with apache's commons httpclient? 当使用NTLM对Sharepoint使用HttpClient身份验证机制时,HTTP 403禁止使用 - HTTP 403 Forbidden coming when using HttpClient authentication mechanism for Sharepoint using NTLM 使用 NTLM 身份验证时,最新的 Apache HttpClient 4.1.1 出现 HTTP/1.1 407 错误 - HTTP/1.1 407 error with latest Apache HttpClient 4.1.1 when using NTLM authentication 在Apache HttpClient 4.3.6上禁用NTLM - Disable NTLM on Apache HttpClient 4.3.6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM