Spring Security Kerberos 示例

本部分參考文件介紹了示例專案。可以透過從 github.com/spring-projects/spring-security-kerberos 構建主分發版來手動編譯示例。

如果您直接執行示例,它將無法工作,直到應用了正確的配置。請參閱下面的特定示例註釋。

安全伺服器端認證示例 使用伺服器端認證器的示例

安全伺服器 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 配置,方法是使用 -Djava.security.krb5.conf=/path/to/krb5.iniGlobalSunJaasKerberosConfig Bean。

有關如何使用 Windows Kerberos 環境的更多說明,請參閱設定 Windows 域控制器

使用域憑據登入 Windows 8.1 並訪問示例

ie1 ie2

從非 Windows 虛擬機器訪問示例應用程式並手動使用域憑據。

ff1 ff2 ff3

安全伺服器端認證示例

本示例演示了伺服器如何透過表單登入傳入使用者的憑據來針對 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

在上面,我們簡單地將 app.user-principalapp.keytab-location 設定為空值,這將停用 keytab 檔案的使用。

如果操作成功,您應該看到以下輸出,其中包含 [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>