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-7.0.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-7.0.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

或使用 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-7.0.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: https://neo.example.org:8080/hello

使用 user1 主體 設定 MIT Kerberos,手動使用憑據進行 kerberos 登入。

$ java -jar sec-client-rest-template-7.0.0.jar --app.user-principal --app.keytab-location

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

如果操作成功,您應該會看到以下輸出,其中包含 [email protected]

<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 [email protected]!</h1>
  </body>
</html>

或者使用帶 keytab 檔案的 user2

$ java -jar sec-client-rest-template-7.0.0.jar

如果操作成功,您應該會看到以下輸出,其中包含 [email protected]

<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 [email protected]!</h1>
  </body>
</html>
© . This site is unofficial and not affiliated with VMware.