我有几台具有不同Auth类型的服务器。基础,NTLM。我需要机制来自动选择它。我看到尝试使用每种凭据类型,并选择成功。我在http客户端4.3中找到了一些方法,命名为impl.client.HttpClientBuilder #setDefaultAuthSchemeRegistry,但是
PS至于现在我对每种类型的auth都有可行的实现。
答案 0 :(得分:1)
可以使用RequestConfig
RequestConfig requestConfig = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.build();
本地执行上下文包含与请求执行有关的所有详细信息,包括目标和代理主机的身份验证状态
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpClientContext localContext = HttpClientContext.create();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget, localContext);
try {
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
AuthState targetAuthState = localContext.getTargetAuthState();
if (targetAuthState.getAuthScheme() != null) {
System.out.println("Target auth scheme: " +
targetAuthState.getAuthScheme().getSchemeName());
}
AuthState proxyAuthState = localContext.getProxyAuthState();
if (proxyAuthState.getAuthScheme() != null) {
System.out.println("Proxy auth scheme: " +
proxyAuthState.getAuthScheme().getSchemeName());
}
} finally {
response.close();
}
} finally {
httpclient.close();
}