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/[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.2.0.jar

您可能需要通过使用 -Djava.security.krb5.conf=/path/to/krb5.iniGlobalSunJaasKerberosConfig bean 来在 Linux 上使用自定义 kerberos 配置。

有关如何使用 Windows kerberos 环境的更多说明,请参阅 设置 Windows 域控制器

使用域凭据登录 Windows 8.1 并访问示例

ie1 ie2

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

ff1 ff2 ff3

安全服务器端认证示例

此示例演示了服务器如何通过表单登录传递的用户凭据,在 kerberos 环境中对用户进行身份验证。

运行服务器。

$ java -jar sec-server-client-auth-2.2.0.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

或者使用密钥表文件。

$ 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.2.0.jar

现在您应该能够打开浏览器并让它使用现有票据进行 Spnego 认证。

有关配置浏览器以使用 Spnego 的更多说明,请参阅 配置浏览器以进行 Spnego 协商

server:
    port: 8080
app:
    service-principal: HTTP/[email protected]
    keytab-location: /tmp/tomcat.keytab

安全客户端 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.2.0.jar --app.user-principal --app.keytab-location

在上面,我们简单地将 app.user-principalapp.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>

或者使用带有密钥表文件的 user2

$ java -jar sec-client-rest-template-2.2.0.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>
© . This site is unofficial and not affiliated with VMware.