【问题标题】:Making HTTP call with Kerberos keytab in Java在 Java 中使用 Kerberos 密钥表进行 HTTP 调用
【发布时间】:2020-03-20 10:08:30
【问题描述】:

我正在尝试向受 Kerberos 身份验证保护的 HTTP 端点发出 GET 请求。我能够使用我的密钥表成功初始化LoginContext,我可以看到正在生成并成功分配给我的SubjectKerberosTicket,但由于某种原因,我的 HTTP 请求仍然返回 401 错误.我怀疑票证本身没有附加到 HTTP 请求,但我不确定如何正确启用它。

作为参考,这是我正在运行的代码。我正在使用 Krb5LoginModule:

        String keyTab = "~/kerberos.keytab";
        String principal = "myself@WEBSITE.COM";
        Subject subject = null;
        try {
            LoginContext context = new LoginContext("", new Subject(), null,
                    new Configuration() {
                        @Override
                        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
                            Map<String, String> options = new HashMap<String, String>();
                            options.put("useKeyTab", "true");
                            options.put("storeKey", "true");
                            options.put("doNotPrompt", "false");
                            options.put("useTicketCache", "true");
                            options.put("isInitiator", "true");
                            options.put("debug", "true");
                            options.put("keyTab", keyTab);
                            options.put("principal", principal);

                            return new AppConfigurationEntry[]{
                                    new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
                                            AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
                                            options)};
                        }
                    });
            context.login(); // Completes successfully. No LoginException thrown.
            subject = context.getSubject();
        }
        catch (LoginException e)
        {
            e.printStackTrace();
            return null;
        }

        String conn = Subject.doAs(subject, new PrivilegedExceptionAction<String>() {

            @Override
            public String run() {
                    URL url = new URL("http://kerberosexample.com");
                    con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("GET");

                    if (con != null) {
                        int status = con.getResponseCode(); // Returns as 401 Unauthenticated.

                        // If status is 200, process response body and return as a String.
                    }
            }
        });

非常感谢任何建议。

【问题讨论】:

  • 我建议您查看此请求的网络跟踪,看看实际发送的内容。
  • 要在 JAAS 中启用调试跟踪(转储到 StdOut / StdErr),请使用 -Dsun.security.krb5.debug=true -Djava.security.debug=gssloginconfig,configfile,configparser‌​,logincontext -Dsun.security.spnego.debug=true >> 输出并不漂亮,正如您对加密库所期望的那样......

标签: java http authentication kerberos spnego


【解决方案1】:

指向正确的 krb5.conf。应该做的工作。 类似System.setProperty("java.security.krb5.conf", "krb5.conf");

【讨论】:

    猜你喜欢
    • 2013-04-08
    • 2019-04-22
    • 2018-02-02
    • 1970-01-01
    • 2014-07-12
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    相关资源
    最近更新 更多