spring security AuthenticationManager vs AuthenticationProvider?

时间:2010-02-24 03:01:15

标签: spring authentication spring-security

有人可以告诉我Spring Security中AuthenticationManagerAuthenticationProvider之间的区别吗?

他们如何使用以及如何调用它们。我的理解是,SecurityFilter会调用AuthenticationManager来验证Authentication对象吗?但是AuthenticationProvider在哪里发挥作用?

谢谢!

3 个答案:

答案 0 :(得分:32)

我认为AuthenticationManager委托将持久用户信息提取到一个或多个AuthenticationProvider。身份验证提供程序(例如DaoAuthenticationProvider, JaasAuthenticationProvider, LdapAuthenticationProvider, OpenIDAuthenticationProvider)专门用于访问特定的用户信息存储库。 参考手册的this part中提到了其他一些内容。它说:

您可能希望使用ProviderManager注册其他AuthenticationProvider bean,您可以使用带有ref属性的元素来执行此操作,其中该属性的值是您要添加的提供者bean的名称。

换句话说,您可以指定多个AuthenticationProviders,例如,一个在LDAP数据库中查找用户,另一个在SQL数据库中查找用户。

答案 1 :(得分:8)

从春季reference

  

AuthenticationManager只是一个接口,因此实现可以是我们选择的任何内容

     

Spring Security中的默认实现称为ProviderManager,它不是处理身份验证请求本身,而是委托给配置的AuthenticationProvider列表,每个查询器依次查询它们是否可以执行身份验证。每个提供程序将抛出异​​常或返回完全填充的Authentication对象。

另外,如果您检查AuthenticationManager,ProviderManager和AuthenticationProvider的源代码,您可以清楚地看到这一点。

ProviderManager实现AuthenticationManager接口,它具有AuthenticationProviders列表。因此,如果您想拥有自定义身份验证机制,则需要实现新的AuthenticationProvider。

答案 2 :(得分:6)

AuthenticationManager和AuthenticationProvider都是接口。它们在Spring Security Flow中具有不同的功能。

Ref-
Spring Boot + Spring Security Architecture

enter image description here

  • AuthenticationManager -当用户尝试访问应用程序时,http请求被过滤器/过滤器链拦截。使用创建的身份验证对象,过滤器将调用身份验证管理器的authenticate方法。 Authentication Manager只是一个接口,ProviderManager提供了authenticate方法的实际实现。ProviderManager包含AuthenticationProviders列表。从它的authenticate方法中,它调用适当的AuthenticateProvider的authenticate方法。作为响应,如果身份验证成功,它将获取主体身份验证对象。

    enter image description here

  • AuthenticationProvider- AuthenicationProvider是具有单一身份验证方法的接口。它具有各种实现,例如CasAuthenticationProvider,DaoAuthenticationProvider。根据实现,使用适当的AuthenicationProvider实现。它是在AuthenticationProvider实现身份验证方法中进行的所有实际身份验证。

    enter image description here

  • 相关问题