请确认您的环境已为 Kerberos 正确设置,这可以通过运行 kinit 来实现。如果失败,您需要确保您的 krb5.ini (windows) 或 krb5.conf (linux) 设置为正确指向您的域控制器。
确认 Kerberos 正常运行后,您可以使用来自 HttpClient 的示例代码,如下所示。
请注意,有许多问题可能会导致 Kerberos 失败,例如时间同步、支持的加密类型、跨域林的信任关系,并且确保您的客户端与服务器位于单独的盒子上也是值得的。
这是 HttpClient 下载中提供的示例代码,您需要确保您的 JAAS 配置和 krb5.conf 或 ini 是正确的!
public class ClientKerberosAuthentication {
public static void main(String[] args) throws Exception {
System.setProperty("java.security.auth.login.config", "login.conf");
System.setProperty("java.security.krb5.conf", "krb5.conf");
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory());
Credentials use_jaas_creds = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1, null),
use_jaas_creds);
HttpUriRequest request = new HttpGet("http://kerberoshost/");
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println("----------------------------------------");
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
System.out.println("----------------------------------------");
// This ensures the connection gets released back to the manager
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}