【发布时间】:2020-03-20 10:08:30
【问题描述】:
我正在尝试向受 Kerberos 身份验证保护的 HTTP 端点发出 GET 请求。我能够使用我的密钥表成功初始化LoginContext,我可以看到正在生成并成功分配给我的Subject 的KerberosTicket,但由于某种原因,我的 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