Spring Security Kerberos 示例
参考文档的这一部分介绍了示例项目。示例可以通过从 github.com/spring-projects/spring-security-kerberos 构建主发行版来手动编译。
:::重要
如果直接运行示例,在应用正确的配置之前它将无法工作。请参阅下方针对特定示例的说明。
:::
安全服务器 Windows 身份验证示例 适用于 Windows 环境
安全服务器端身份验证示例 使用服务器端身份验证器的示例
安全服务器 Spnego 和表单认证示例 使用 spnego 和表单进行票据验证的示例
安全客户端 KerberosRestTemplate 示例 用于 KerberosRestTemplate 的示例
安全服务器 Windows 身份验证示例
本示例的目标:
-
在 Windows 环境下,用户应能使用登录 Windows 时输入的 Windows Active Directory 凭据登录应用程序。系统不应再要求用户输入用户 ID/密码凭据。
-
在非 Windows 环境下,系统将向用户呈现一个界面,要求其提供 Active Directory 凭据。
server:
port: 8080
app:
ad-domain: EXAMPLE.ORG
ad-server: ldap://WIN-EKBO0EQ7TS7.example.org/
service-principal: HTTP/neo.example.org@EXAMPLE.ORG
keytab-location: /tmp/tomcat.keytab
ldap-search-base: dc=example,dc=org
ldap-search-filter: "(| (userPrincipalName={0}) (sAMAccountName={0}))"
如上所示,您可以看到此示例的默认配置。您可以使用常规的 Spring Boot 技巧来覆盖这些设置,例如使用命令行选项或自定义的 application.yml 文件。
运行服务器。
$ java -jar sec-server-win-auth-7.0.2.jar
:::重要
在 Linux 环境下,您可能需要使用自定义的 Kerberos 配置,可以通过 -Djava.security.krb5.conf=/path/to/krb5.ini 参数或 GlobalSunJaasKerberosConfig Bean 来实现。
:::
有关如何在 Windows Kerberos 环境中工作的更多说明,请参阅设置 Windows 域控制器。
使用域凭据登录 Windows 8.1 并访问示例


从非 Windows 虚拟机访问示例应用程序,并手动使用域凭据。



安全服务器端身份验证示例
此示例演示了服务器如何通过表单登录传递的用户凭证,在Kerberos环境中对用户进行身份验证。
运行服务器。
$ java -jar sec-server-client-auth-7.0.2.jar
server:
port: 8080
安全服务器 SPNEGO 和表单认证示例
本示例演示了如何配置服务器以接受来自浏览器的基于Spnego的协商,同时仍能回退到基于表单的身份验证。
使用 user1 主体 设置 MIT Kerberos,手动使用凭据进行 Kerberos 登录。
$ kinit user1
Password for user1@EXAMPLE.ORG:
$ klist
Ticket cache: FILE:/tmp/krb5cc_1000
Default principal: user1@EXAMPLE.ORG
Valid starting Expires Service principal
10/03/15 17:18:45 11/03/15 03:18:45 krbtgt/EXAMPLE.ORG@EXAMPLE.ORG
renew until 11/03/15 17:18:40
或使用密钥表文件。
$ kinit -kt user2.keytab user1
$ klist
Ticket cache: FILE:/tmp/krb5cc_1000
Default principal: user2@EXAMPLE.ORG
Valid starting Expires Service principal
10/03/15 17:25:03 11/03/15 03:25:03 krbtgt/EXAMPLE.ORG@EXAMPLE.ORG
renew until 11/03/15 17:25:03
运行服务器。
$ java -jar sec-server-spnego-form-auth-7.0.2.jar
现在,您应该能够打开浏览器,让它使用现有的票据进行 Spnego 认证。
有关配置浏览器以使用 Spnego 的更多说明,请参阅配置浏览器进行 Spnego 协商。
server:
port: 8080
app:
service-principal: HTTP/neo.example.org@EXAMPLE.ORG
keytab-location: /tmp/tomcat.keytab
安全客户端 KerberosRestTemplate 示例
这是一个使用 Spring RestTemplate 访问受 Kerberos 保护资源的示例。你可以将其与 Security Server Spnego 和表单认证示例 结合使用。
默认应用程序配置如下所示。
app:
user-principal: user2@EXAMPLE.ORG
keytab-location: /tmp/user2.keytab
access-url: https://neo.example.org:8080/hello
使用 user1 主体 设置 MIT Kerberos,手动使用凭据进行 Kerberos 登录。
$ java -jar sec-client-rest-template-7.0.2.jar --app.user-principal --app.keytab-location
在上文中,我们简单地将 app.user-principal 和 app.keytab-location 设置为空值,这将禁用 keytab 文件的使用。
如果操作成功,您应该会看到如下输出,其中包含 user1@EXAMPLE.ORG。
<html xmlns="https://www.w3.org/1999/xhtml"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Kerberos Example</title>
</head>
<body>
<h1>Hello user1@EXAMPLE.ORG!</h1>
</body>
</html>
或者使用带有密钥表文件的 user2。
$ java -jar sec-client-rest-template-7.0.2.jar
如果操作成功,您应该会看到如下输出,其中包含 user2@EXAMPLE.ORG。
<html xmlns="https://www.w3.org/1999/xhtml"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Kerberos Example</title>
</head>
<body>
<h1>Hello user2@EXAMPLE.ORG!</h1>
</body>
</html>