我正在尝试使用Apache / Jakarta HttpClient 4.1.1使用给定的凭据连接到任意网页.为了测试这一点,我在运行的dev机器上安装了最少的IIS 7.5,其中一次只有一种身份验证模式处于活动状态.基本身份验证工作正常,但每当我尝试登录时,Digest和NTLM都会返回401错误消息.这是我的代码:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://localhost/");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new NTCredentials("user", "password", "", "localhost"));
if (!new File(System.getenv("windir") + "\\krb5.ini").exists()) {
List<String> authtypes = new ArrayList<String>();
authtypes.add(AuthPolicy.NTLM);
authtypes.add(AuthPolicy.DIGEST);
authtypes.add(AuthPolicy.BASIC);
httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF,
authtypes);
httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,
authtypes);
}
localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
HttpResponse response = httpclient.execute(httpget, localContext);
System.out.println("Response code: " + response.getStatusLine());
我在Fiddler中注意到的一件事是Firefox和HttpClient发送的哈希值不同,这让我觉得IIS 7.5可能比HttpClient提供更强的哈希值?有任何想法吗?如果我能够验证这适用于NTLM,那就太棒了.摘要也会很好,但如果有必要,我可以不用它.
解决方法:
我不是这方面的专家,但在使用http组件的NTLM身份验证期间,我看到客户端需要3次尝试才能连接到我的情况下的NTML端点.对于Spnego来说有点描述here但是对于NTLM身份验证它有点不同.
对于第一次尝试的NTLM,客户端将使用目标身份验证状态发出请求:UNCHALLENGED和Web服务器返回HTTP 401状态和标头:WWW-Authenticate:NTLM
客户端将检查配置的身份验证方案,应在客户端代码中配置NTLM.
第二次尝试,客户端将使用Target auth state:CHALLENGED发出请求,并将发送一个带有以base64格式编码的令牌的授权头:授权:NTLM TlRMTVNTUAABAAAAAYIIogAAAAAAAAAAAAAACAAAAAAAAAAAAAAAADA =
服务器再次返回HTTP 401状态,但标题:WWW-Authenticate:NTLM现在填充了编码信息.
第三次尝试客户端将使用来自WWW-Authenticate:NTLM标头的信息,并将使用目标身份验证状态:HANDSHAKE和授权标头授权:NTLM,其中包含服务器的更多信息.
在我的情况下,我收到HTTP / 1.1 200 OK.
为了避免在章节4.7.1中的每个请求documentation中的所有这一切,请说明必须将相同的执行令牌用于逻辑相关的请求.对我来说它没有用.
我的代码:
我在EJB的@PostConstruct方法中初始化客户端一次
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(18);
cm.setDefaultMaxPerRoute(6);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(30000)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new NTCredentials(userName, password, hostName, domainName));
// Finally we instantiate the client. Client is a thread safe object and can be used by several threads at the same time.
// Client can be used for several request. The life span of the client must be equal to the life span of this EJB.
this.httpclient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultCredentialsProvider(credentialsProvider)
.setDefaultRequestConfig(requestConfig)
.build();
在每个请求中使用相同的客户端实例:
HttpPost httppost = new HttpPost(endPoint.trim());
// HttpClientContext is not thread safe, one per request must be created.
HttpClientContext context = HttpClientContext.create();
response = this.httpclient.execute(httppost, context);
在我的EJB的@PreDestroy方法中释放资源并将连接返回给连接管理器:
this.httpclient.close();