2010-02-24 34 views
38

有人能告訴我在Spring Security中AuthenticationManagerAuthenticationProvider之間的區別嗎?spring security AuthenticationManager vs AuthenticationProvider?

它們是如何使用的以及它們是如何被調用的。我的理解是,SecurityFilter將調用AuthenticationManager來驗證Authentication對象?但是,AuthenticationProvider在哪裏起作用?

謝謝!

回答

25

我認爲AuthenticationManager委託獲取持久用戶信息到一個或多個AuthenticationProvider s。身份驗證提供程序(例如,DaoAuthenticationProvider, JaasAuthenticationProvider, LdapAuthenticationProvider, OpenIDAuthenticationProvider)專門用於訪問特定的用戶信息存儲庫。 參考手冊的this part中提到了其他內容。它說:

您可能希望與ProviderManager的註冊額外的AuthenticationProvider豆,你可以使用元素與ref屬性,當屬性的值是您要添加的提供商bean的名字做到這一點。

換句話說,您可以指定多個AuthenticationProviders,例如查找LDAP數據庫中的用戶和查找SQL數據庫中的另一個用戶。

+0

所以,你很少需要實現一個AuthenticationManager,而是你只需要實現AuthenticationProviders來從你想要獲取它們的地方獲取用戶詳細信息? – 2010-02-25 22:18:20

+0

您可能需要的大部分AuthenticationProviders已經由編寫Spring Security的人編寫,並且正好在API文檔中,因此您可以配置它們。 如果你需要的不在框架中,那麼爲你的目的實現AuthenticationProvider接口可能是微不足道的。 – 2010-02-25 23:37:21

+0

正如Hans所說, 在Sprint Security中,類ProviderManager是AuthenticationManager的一個實現,它調用AuthenticationProvider的身份驗證方法。 該提供程序管理器的方法簽名是 org.springframework.security.providers.ProviderManager.doAuthentication(身份驗證) – Rajesh 2014-03-28 08:47:11

3

從春天reference

的好的AuthenticationManager只是一個接口,所以它的實現可以讓我們隨便選擇

Spring Security的默認實現被稱爲ProviderManager不只是處理授權請求自己,它將委託給已配置的AuthenticationProvider列表,每個列表都會依次查詢以查看是否可以執行身份驗證。每個提供者都會拋出一個異常或返回完全填充的認證對象。

此外,如果你檢查AuthenticationManager,ProviderManager和AuthenticationProvider的源代碼,你可以清楚地看到這一點。

ProviderManager實現了AuthenticationManager接口,它具有AuthenticationProviders列表。所以如果你想擁有自定義認證機制,你需要實現新的AuthenticationProvider。

相關問題