我正在使用 Kerberos 身份验证编写 HTTP 连接。我有“HTTP/1.1 401 未经授权”。你能推荐我应该检查什么吗?我认为有一些诡计,但我没有看到。
也许我应该将 header “WWW-Authenticate”设置为“Negotiate”?
非常感谢您提供的任何帮助和想法。
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 {
NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
List<String> authpref = new ArrayList<String>();
authpref.add(AuthPolicy.BASIC);
authpref.add(AuthPolicy.SPNEGO);
httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1, AuthScope.ANY_REALM, AuthPolicy.SPNEGO),
new UsernamePasswordCredentials("myuser", "mypass"));
System.out.println("----------------------------------------");
HttpUriRequest request = new HttpGet("http://localhost:8084/web-app/webdav/213/_test.docx");
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 {
httpclient.getConnectionManager().shutdown();
}
}
}
最佳答案
SPNEGO 将无法工作,因为您使用 localhost
作为 URL 主机名。
您的服务器配置了一组 SPN(或至少一个),以在 ActiveDirectory 服务帐户上注册的 HTTP/
开头。感谢 setspn -l yourServiceAccount
,您可以从 AD 查询它们。
您的 URL 必须使用在 ActiveDirectory 中称为 SPN 的有效服务器主机名,以便 Apache Http 客户端可以为此服务协商 TGS 并将其发送到您的服务器。
关于java - 使用 HTTP 客户端的 Kerberos 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9885323/