我正在尝试对运行 IIS 的 Windows 服务器进行身份验证,该服务器使用 Apache HttpClient 4.3 为 Windows 集成身份验证 (SPNEGO) 配置。我的代码看起来与我能够在网上找到的示例代码非常相似,但是当我运行它时,我始终会返回 HTTP 401。我在结果上运行了 Wireshark,并没有看到 SPNEGO 令牌被传递到服务器。
我可以通过网络浏览器很好地访问受保护的资源,在这种情况下,我确实看到了 SPNEGO 令牌。但是,当我运行我的代码时,行为是不同的。这是有问题的代码:
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 文档。如文档中所述,我还进行了适当的注册表项更改。
知道是什么原因造成的,或者我该如何进行故障排除?有没有我遗漏的步骤或线条?