【问题标题】:SPNEGO Authentication via HttpClient通过 HttpClient 进行 SPNEGO 身份验证
【发布时间】:2014-02-24 18:38:48
【问题描述】:

我正在尝试使用 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 文档。如文档中所述,我还进行了适当的注册表项更改。

知道可能是什么原因造成的,或者我可以如何进行故障排除?有没有我遗漏的步骤或行?

【问题讨论】:

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


    【解决方案1】:

    问题解决了。这似乎是由于 IBM 的 JDK 中的一个错误造成的。换成 Sun 后,一切正常。

    【讨论】:

    • ibm jdk 与 sun jdk 相比具有不同的 jaas 参数。你确定你使用的是正确的吗? IBM JDK 没有问题,我有 HTTP 客户端来使用它。
    • 已与 IBM 确认。这是他们的 JVM 版本中的一个错误。
    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多