使用HTTP客户端的Kerberos连接

Ser*_*lov 7 java authentication kerberos apache-commons-httpclient

我正在使用Kerberos身份验证编写HTTP连接.我有"HTTP/1.1 401 Unauthorized".你能推荐我应该检查一下吗?我认为有一些想法,但我没有看到它.

可能是我应该用"Negotiate"设置标题"WWW-Authenticate"?

非常感谢您提供任何帮助和想法.

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SCB*_*Boy 0

我也遇到了同样的问题,刚刚找到你的帖子。我已将其添加为书签,以便修复后可以发布答案。我发布了一个链接到我的问题,有人回答了它,所以如果有人通过谷歌找到这个,他们就会找到答案:

当 URL 具有端口时,HttpClient 在为 AD 创建 SPN 时出现问题。

请在此处查看我的问题+答案:HttpClient check Kerberos secure website。NTLM 登录不起作用