我正在尝试使用 Apache HttpClient 4.3 向运行 IIS 的 Windows 服务器进行身份验证,该服务器配置为使用 Apache HttpClient 4.3 进行 Windows 集成身份验证 (SPNEGO)。我的代码看起来与我在网上找到的示例代码非常相似,但是当我运行它时,我始终会返回 HTTP 401。我对结果运行了 Wireshark,但没有看到 SPNEGO token 被传递到服务器。
我可以通过 Web 浏览器访问 protected 资源,在这种情况下,我确实看到了 SPNEGO token 。但是,当我运行我的代码时,行为是不同的。这是有问题的代码:
public static void main(String[] args) {
System.setProperty("java.security.krb5.conf",
"c:\\develop\\XYZ\\KerberosTest\\conf\\krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
System.setProperty("java.security.auth.login.config",
"c:\\develop\\XYZ\\KerberosTest\\conf\\login.conf");
Credentials jaasCredentials = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1, null),
jaasCredentials);
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder
.<AuthSchemeProvider> create().register(AuthSchemes.SPNEGO,
new SPNegoSchemeFactory()).build();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.setDefaultCredentialsProvider(credsProvider).build();
try {
HttpGet httpget = new HttpGet(ENDPOINT);
RequestLine requestLine = httpget.getRequestLine();
CloseableHttpResponse response = httpclient.execute(httpget);
try {
StatusLine status = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (entity != null) {
}
EntityUtils.consume(entity);
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我相信我已经正确配置了我的 krb5.conf 文件,我的 login.conf 文件直接取自 Apache HttpClient 文档。如文档中所述,我还进行了适当的注册表项更改。
知道是什么原因造成的,或者我该如何进行故障排除?有没有我遗漏的步骤或行?
最佳答案
问题解决了。这似乎是由于 IBM 的 JDK 中的一个错误。一旦我换成 Sun 的,一切正常。
关于java - 通过 HttpClient 进行 SPNEGO 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996162/