Spring Security Kerberos 示例
本部分参考文档介绍了示例项目。可以通过从 github.com/spring-projects/spring-security-kerberos 构建主分发版来手动编译示例。
如果您直接运行示例,它将无法工作,直到应用了正确的配置。请参阅下面的特定示例注释。 |
安全服务器 Windows 认证示例 Windows 环境示例
安全服务器端认证示例 使用服务器端认证器的示例
安全服务器 Spnego 和表单认证示例 使用 Spnego 和表单进行票据验证的示例
安全服务器 Spnego 和表单认证 XML 示例 使用 Spnego 和表单进行票据验证的示例(XML 配置)
安全客户端 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/[email protected]
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-2.1.1.jar
在 Linux 上,您可能需要使用自定义 Kerberos 配置,方法是使用 |
有关如何使用 Windows Kerberos 环境的更多说明,请参阅设置 Windows 域控制器。 |
使用域凭据登录 Windows 8.1
并访问示例
从非 Windows 虚拟机访问示例应用程序并手动使用域凭据。
安全服务器端认证示例
本示例演示了服务器如何通过表单登录传入用户的凭据来针对 Kerberos 环境验证用户身份。
运行服务器。
$ java -jar sec-server-client-auth-2.1.1.jar
server:
port: 8080
安全服务器 Spnego 和表单认证示例
本示例演示了如何配置服务器以接受来自浏览器的基于 Spnego 的协商,同时仍能回退到基于表单的认证。
使用 user1
主体,设置 MIT Kerberos,使用凭据手动执行 Kerberos 登录。
$ kinit user1
Password for [email protected]:
$ klist
Ticket cache: FILE:/tmp/krb5cc_1000
Default principal: [email protected]
Valid starting Expires Service principal
10/03/15 17:18:45 11/03/15 03:18:45 krbtgt/[email protected]
renew until 11/03/15 17:18:40
或使用 keytab 文件。
$ kinit -kt user2.keytab user1
$ klist
Ticket cache: FILE:/tmp/krb5cc_1000
Default principal: [email protected]
Valid starting Expires Service principal
10/03/15 17:25:03 11/03/15 03:25:03 krbtgt/[email protected]
renew until 11/03/15 17:25:03
运行服务器。
$ java -jar sec-server-spnego-form-auth-2.1.1.jar
现在您应该能够打开浏览器,让它使用现有票据进行 Spnego 认证。
有关配置浏览器使用 Spnego 的更多说明,请参阅配置浏览器以进行 Spnego 协商。 |
server:
port: 8080
app:
service-principal: HTTP/[email protected]
keytab-location: /tmp/tomcat.keytab
安全服务器 Spnego 和表单认证 XML 示例
这与安全服务器 Spnego 和表单认证示例是相同的示例,但使用基于 XML 的配置而不是 JavaConfig。
运行服务器。
$ java -jar sec-server-spnego-form-auth-xml-2.1.1.jar
安全客户端 KerberosRestTemplate 示例
这是使用 Spring RestTemplate 访问 Kerberos 保护资源的示例。您可以将其与安全服务器 Spnego 和表单认证示例一起使用。
默认应用程序配置如下所示。
app:
user-principal: [email protected]
keytab-location: /tmp/user2.keytab
access-url: http://neo.example.org:8080/hello
使用 user1
主体,设置 MIT Kerberos,使用凭据手动执行 Kerberos 登录。
$ java -jar sec-client-rest-template-2.1.1.jar --app.user-principal --app.keytab-location
在上面,我们简单地将 |
如果操作成功,您应该看到以下输出,其中包含 [email protected]
。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Kerberos Example</title>
</head>
<body>
<h1>Hello [email protected]!</h1>
</body>
</html>
或使用带有 keytab 文件的 user2
。
$ java -jar sec-client-rest-template-2.1.1.jar
如果操作成功,您应该看到以下输出,其中包含 [email protected]
。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Kerberos Example</title>
</head>
<body>
<h1>Hello [email protected]!</h1>
</body>
</html>