Google Chrome 和 IE 等应用程序可以透明地处理 Kerberos 身份验证;但是我找不到一个“简单”的 Java 解决方案来匹配这种透明度。我发现的所有解决方案都需要存在 krb5.conf 文件和 login.conf 文件,而上述应用程序似乎都不需要。
构建具有正常运行的 Kerberos SSO 功能的 Java 应用程序的最佳方法是什么?
[更新]:需要明确的是,我需要一个客户端解决方案来创建票证而不是验证它们。另外,SPNEGO 似乎是默认的“包装”协议,最终将委托给 Kerberos,但我也需要能够处理 SPNEGO 协议。
Applications such a Google's Chrome and IE can transparently handle Kerberos authentication; however I can not find a "simple" Java solution to match this transparency. All of the solutions I have found require the presence of a krb5.conf file and a login.conf file which nether of the above apps seem to require.
What is the best way to build a Java application with Kerberos SSO capabilities that just work?
[update]: to be clear I need a CLIENT side solution for creating tickets not validating them. Also, it seems that SPNEGO is the default "wrapper" protocol that will eventually delegate to Kerberos but I need to be able to handle the SPNEGO protocol as well.
发布评论
评论(9)
现在有一个简单的解决方案,使用 Apache HTTP 组件客户端 4.5 或更高版本。这在 4.5 中仍然被标记为实验性的,因此您的情况可能会有所不同,但这对我来说在企业环境中工作得很好。
除了 HC 4.5 客户端 jar 之外,您还需要在类路径上拥有 httpclient-win、jna 和 jna-platform jar,如 http-component-client 提供的那样。然后,您可以按如下方式构建启用 Kerberos 的 HC 客户端:
或者使用构建器:
然后可以在构建客户端之前根据需要进行自定义:
该解决方案无需任何外部配置即可工作,最重要的是解决了内置 JRE 机制的问题在 Windows 7+ 上具有本地管理员权限的用户会中断。这是可能的,因为 Kerberos 票证是通过 JNA 直接从 SSPI API 检索的,而不是通过 JRE 提供的 GSSAPI。
示例来自 http-components 团队的代码
这一切都归功于 Daniel Doubrovkine 蒂莫西·沃尔
和 瑞恩·麦金利
There is now a simple solution for this using the Apache HTTP Components Client 4.5 or greater. This is still marked as experimental in 4.5 so your milage may vary, but this is working fine for me in an enterprise context.
In addition to the HC 4.5 client jars you will need to have the httpclient-win, jna and jna-platform jars on your classpath, as provided with http-component-client. You then construct a Kerberos enabled HC-client as follows:
Or using the builder:
Which can then be customised as required before building the client:
This solution works without any external configuration, and most importantly solves the issue where the in-built JRE mechanism breaks for users with local Admin rights on Windows 7+. This is possible because the Kerberos ticket is being retrieved directly from the SSPI API via JNA, rather than going through the GSSAPI provided by the JRE.
Example code from the http-components team
This was all made possible by the good work of Daniel Doubrovkine Timothy Wall
and Ryan McKinley
添加到 David Roussels 关于基于 url 的特定 http 基于 kerberos 身份验证的答案:-
您的代码有效的原因是因为您的目标 SPN(服务器端主体)配置为 HTTP/[电子邮件受保护]。在这种情况下,它会起作用,因为您没有显式设置令牌。 URLConnection 在内部设置具有该 SPN 的令牌
1 执行步骤(来自我之前的答案)以获取主题
2 使用 gss api init sec context 生成上下文令牌。对于此步骤,有很多教程
3 对令牌进行 Base 64 编码
4 将令牌附加到 urlconnection:-
5 实施特权操作:-
6 将整个内容包装在 subject.doAs 中
Adding to David Roussels answer on url specific http based kerberos authentication:-
The reason why your code works is because your target SPN(server side principal) is configured to with HTTP/[email protected]. In that case it will work because you are not explicitly setting the token. URLConnection internally sets a token with that SPN
1 Perform steps(from my previous answer) to get a subject
2 Use gss api init sec context to generate a context token. There are numerous tutorials out there for this step
3 Base 64 encode the token
4 Attach the token to urlconnection:-
5 Implement a priviledged action:-
6 Wrap the whole thing in Subject.doAs
Oracle 有使用 Java 的 SaslClient 的示例。我不是 Java 程序员,但是当我向某个 Java 程序员指出这一点时,他们能够很快地使其工作。它可能仍然需要某个地方的“conf”文件(注意 Kerberos 使用环境变量,通常以 KRB5_ 开头,以知道在哪里查找此类文件)。另请注意,Kerberos 本身不包含任何类型的传输 - 您的应用程序需要知道如何按照服务器期望的方式发送和接收 Kerberos 有效负载(这根据您尝试进行身份验证的服务器而有所不同)。
编辑:您编辑了您的问题,所以这里有一个与 Java 中的 SPNEGO 相关的链接,可能会有一些用处:
http://download.oracle.com/javase/6/文档/technotes/guides/security/jgss/lab/part5.html
Oracle has an example using Java's SaslClient. I'm not a Java programmer, but when I pointed this out once to someone who is, they were able to make it work pretty quickly. It may still require a "conf" file somewhere (n.b. Kerberos uses environment variables, often starting with KRB5_, to know where to look for such files). Also note that Kerberos itself does not include a transport of any kind--your app needs to know how to send and receive the Kerberos payloads the way the server expects (and this is different depending on the server you are trying to authenticate with).
Edit: you edited your question, so here's a link related to SPNEGO in Java which might be of some use:
http://download.oracle.com/javase/6/docs/technotes/guides/security/jgss/lab/part5.html
你实际上不需要做任何事情。在 Java 6 中,在 Windows 客户端计算机上,您可以执行以下操作:
并且协商身份验证正常工作。至少对我来说是这样。而且我测试的服务器只支持Negotiate,不支持NTLM auth。
You don't actually need to do anything. In Java 6, on a Windows client machine you can do this:
And negotiate authentication just works. At least it does for me. And the server I tested on only supports Negotiate, not NTLM auth.
好的,如果您想避免使用 login.conf 文件,您需要
以不同的方式进行编码:- 现在,一旦您完成了此定义,您就可以使用它来从 kdc 获取票证
现在从这里开始,您可以获取 jaas 主题和基本上可以做大量的身份验证工作。
如果您需要进一步的指导,请发表评论。
Ok if you want to avoid using a login.conf file you need to code differently:-
Now once you are done with this definition you can use this in you use this to fetch tickets from kdc
Now from here on you can fetch jaas subject and can basically do a ton of authentication stuff.
In case you need further pointers just leave a comment.
您可以使用系统属性而不是配置文件来指定 KDC 主机名和服务名称,但这些东西(至少)是强制性的......
Waffle 实际上会为您提供设置大多数属性所需的信息,即使它不会给你一张票。查看 WindowsAuthProviderImpl 类(Waffle.chm 帮助文件显示了 API)。
我使用 JAAS 分两步从 Active Directory 获取服务票证:
使用 Krb5LoginModule 检索缓存的 TGT 并将其添加到主题。
使用主题和 GSS-API 从 KDC 检索服务票证。
The Java Way of Active Directory 中有很多很好的信息和示例代码。
You can use system properties instead of config files to specify the KDC hostname and service name, but those things (at least) are mandatory....
Waffle will actually give you the information you need to set most of the properties, even if it won't get you a ticket. Look at the WindowsAuthProviderImpl class (the Waffle.chm help file shows the API).
I use JAAS do obtain a service ticket from Active Directory in two steps:
Use Krb5LoginModule to retrieve the cached TGT and add it to the Subject.
Use the Subject and GSS-API to retrieve a service ticket from the KDC.
There's a lot of good information and example code at The Java Way of Active Directory.
我创建了一个小工具来简化 httpclient 到 kerberos 的连接,您可能想尝试一下。
https://github.com/DovAmir/httpclientAuthHelper
I created a small tool to simplify connecting with httpclient to kerberos, you might want to give it a try.
https://github.com/DovAmir/httpclientAuthHelper
使用 WAFFLE
Use WAFFLE
这是一篇关于使用 Kerberos 的 Java 客户端的优秀博客文章
http://sachithdhanushka.blogspot.com/2014/02/kerberos -java-client-configuration.html
Here's a good blog post on having a java client to use with Kerberos
http://sachithdhanushka.blogspot.com/2014/02/kerberos-java-client-configuration.html