java - 通过 HttpClient 进行 SPNEGO 身份验证

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

我正在尝试使用 Apache HttpClient 4.3 向运行 IIS 的 Windows 服务器进行身份验证,该服务器配置为使用 Apache HttpClient 4.3 进行 Windows 集成身份验证 (SPNEGO)。我的代码看起来与我在网上找到的示例代码非常相似,但是当我运行它时,我始终会返回 HTTP 401。我对结果运行了 Wireshark,但没有看到 SPNEGO token 被传递到服务器。

我可以通过 Web 浏览器访问 protected 资源,在这种情况下,我确实看到了 SPNEGO token 。但是,当我运行我的代码时,行为是不同的。这是有问题的代码:

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

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

最佳答案

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

关于java - 通过 HttpClient 进行 SPNEGO 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996162/

相关文章:

java - 每次单元测试后回滚

asp.net - 添加对已部署 ASP.NET 站点的程序集引用

asp.net - 如何修改 web.config 以不重写某个 url?

iis - 将方案从 gzip 更改为 deflate 是否可以修复 ie6 不兼容问题?

使用 Kerberos 和 LDAP 的 Java Spring SSO 授权

python-2.7 - Kerberos 安装后 Python Impyla 失败

java - 为什么我们需要在加载 .class 文件后重新启动 Tomcat?

java - 如何将字符串转换为字节数组?

Java Kerberos 身份验证

java - Cucable插件: How can I avoid running the '[CUCABLE:FEATURE].feature' (java template file)?