驗證服務

這會建立 Spring Security 的 ProviderManager 類別的實例,該實例需要設定一個或多個 AuthenticationProvider 實例的列表。這些可以使用命名空間提供的語法元素建立,也可以是標準 Bean 定義,並使用 authentication-provider 元素標記為添加到列表中。

<authentication-manager>

每個使用命名空間的 Spring Security 應用程式都必須在某處包含此元素。它負責註冊為應用程式提供驗證服務的 AuthenticationManager。所有建立 AuthenticationProvider 實例的元素都應該是此元素的子元素。

<authentication-manager> 屬性

  • alias 此屬性允許您為內部實例定義別名,以便在您自己的組態中使用。

  • erase-credentials 如果設定為 true,AuthenticationManager 將嘗試清除返回的 Authentication 物件中的任何憑證資料,一旦使用者通過驗證。實際上,它對應於 ProviderManagereraseCredentialsAfterAuthentication 屬性。

  • observation-registry-ref 對於 FilterChain 和相關組件使用的 ObservationRegistry 的參考

  • id 此屬性允許您為內部實例定義 ID,以便在您自己的組態中使用。它與 alias 元素相同,但為使用 id 屬性的元素提供了更一致的體驗。

<authentication-manager> 的子元素

<authentication-provider>

除非與 ref 屬性一起使用,否則此元素是設定 DaoAuthenticationProvider 的簡寫。DaoAuthenticationProviderUserDetailsService 加載使用者資訊,並將使用者名稱/密碼組合與登入時提供的值進行比較。UserDetailsService 實例可以通過使用可用的命名空間元素 (jdbc-user-service) 或通過使用 user-service-ref 屬性指向應用程式上下文中其他地方定義的 Bean 來定義。

<authentication-provider> 的父元素

<authentication-provider> 屬性

  • ref 定義對實作 AuthenticationProvider 的 Spring Bean 的參考。

如果您已經編寫了自己的 AuthenticationProvider 實作 (或者由於某些原因想要將 Spring Security 自己的實作設定為傳統 Bean),那麼您可以使用以下語法將其添加到 ProviderManager 的內部列表中

<security:authentication-manager>
  <security:authentication-provider ref="myAuthenticationProvider" />
</security:authentication-manager>
<bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider"/>
  • user-service-ref 對於實作 UserDetailsService 的 Bean 的參考,該 Bean 可以使用標準 Bean 元素或自訂 user-service 元素建立。

<authentication-provider> 的子元素

<jdbc-user-service>

導致建立基於 JDBC 的 UserDetailsService。

<jdbc-user-service> 屬性

  • authorities-by-username-query 給定使用者名稱,查詢使用者授權的 SQL 語句。

預設值為

select username, authority from authorities where username = ?
  • cache-ref 定義對快取記憶體的參考,以用於 UserDetailsService。

  • data-source-ref 提供所需表格的 DataSource 的 Bean ID。

  • group-authorities-by-username-query 給定使用者名稱,查詢使用者群組授權的 SQL 語句。預設值為

    select
    g.id, g.group_name, ga.authority
    from
    groups g, group_members gm, group_authorities ga
    where
    gm.username = ? and g.id = ga.group_id and g.id = gm.group_id
  • id Bean 識別符,用於在上下文中的其他地方引用 Bean。

  • role-prefix 將添加到從持久性儲存加載的角色字串的非空字串前綴 (預設為 "ROLE_")。在預設值為非空的情況下,對於沒有前綴的情況,請使用值 "none"。

  • users-by-username-query 給定使用者名稱,查詢使用者名稱、密碼和啟用狀態的 SQL 語句。預設值為

    select username, password, enabled from users where username = ?

<password-encoder>

可以選擇設定驗證提供者以使用密碼編碼器,如 密碼儲存 中所述。這將導致 Bean 被注入適當的 PasswordEncoder 實例。

<password-encoder> 的父元素

<password-encoder> 屬性

  • hash 定義在使用者密碼上使用的雜湊演算法。我們強烈建議不要使用 MD4,因為它是一種非常弱的雜湊演算法。

  • ref 定義對實作 PasswordEncoder 的 Spring Bean 的參考。

<user-service>

從屬性檔案或 "user" 子元素列表建立記憶體中的 UserDetailsService。使用者名稱在內部轉換為小寫以允許不區分大小寫的查找,因此如果需要區分大小寫,則不應使用此功能。

<user-service> 屬性

  • id Bean 識別符,用於在上下文中的其他地方引用 Bean。

  • properties 屬性檔案的位置,其中每行格式為

    username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]

<user-service> 的子元素

<user>

表示應用程式中的使用者。

<user> 的父元素

<user> 屬性

  • authorities 授予使用者的一個或多個授權。用逗號 (但沒有空格) 分隔授權。例如,"ROLE_USER,ROLE_ADMINISTRATOR"

  • disabled 可以設定為 "true" 以將帳戶標記為停用且無法使用。

  • locked 可以設定為 "true" 以將帳戶標記為鎖定且無法使用。

  • name 分配給使用者的使用者名稱。

  • password 分配給使用者的密碼。如果相應的驗證提供者支援雜湊,則可以對其進行雜湊 (記住設定 "user-service" 元素的 "hash" 屬性)。如果資料不用於驗證,而僅用於存取授權,則可以省略此屬性。如果省略,命名空間將產生一個隨機值,以防止意外用於驗證。不能為空。