通过HttpClient进行SPNEGO身份验证

时间:2014-02-24 18:38:48

标签: java iis kerberos apache-httpclient-4.x spnego

我正在尝试使用Apache HttpClient 4.3对运行IIS的Windows服务器进行身份验证,该服务器是为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文档。我还做了相应的注册表项更改,如文档中所述。

知道可能导致此问题的原因或我如何进行故障排除?我缺少一个步骤或行吗?

1 个答案:

答案 0 :(得分:-1)

问题解决了。这似乎是由于IBM JDK中的一个错误。一旦我改为Sun's,一切正常。