在 Spring Security 中使用 HttpSecurity 与 AuthenticationManagerBuilder 注册身份验证提供者有什么区别?

Jaz*_*epi 8 spring-security spring-boot

WebSecurityConfigurerAdapter 提供两种覆盖,如下所示:

protected void configure(AuthenticationManagerBuilder auth)

protected void configure(HttpSecurity http)

两者HttpSecurityAuthenticationManagerBuilder提供身份验证提供商的注册。注册我的提供商与另一提供商之间有什么区别吗?

我还使用 Spring boot 2.1 来@SpringBootApplication(exclude = SecurityAutoConfiguration.class)完全关闭它们的自动配置。

And*_*sha 7

Spring Security 架构

\n\n
\n

身份验证的主要策略接口是\n AuthenticationManager[...]

\n\n

最常用的实现AuthenticationManager是\n ProviderManager,它委托给\n 实例链 AuthenticationProvider。AnAuthenticationProvider有点像AuthenticationManager[...]

\n\n

AProviderManager可以通过委托链来支持同一应用程序中的多种不同的身份验证机制 AuthenticationProviders。如果 aProviderManager无法识别特定Authentication实例类型,则它将被跳过。

\n\n

AProviderManager有一个可选的父级,如果所有提供者都返回 null,则 A 可以咨询该父级。如果父级不可用,则 null\n Authentication会导致AuthenticationException.

\n
\n\n

在此输入图像描述

\n\n

一般来说,除了WebSecurityConfigurerAdapter提供配置(如等)之外,它还通过使用创建和配置(添加和父级)。HttpSecurityFilterUsernamePasswordAuthenticationFilterLogoutFilterAuthenticationProviderAuthenticationManagerAuthenticationManagerHttpSecurityAuthenticationManagerBuilder

\n\n

WebSecurityConfigurerAdapterAuthenticationManager将为只创建一个HttpSecurity。然而AuthenticationManager有它自己的AuthenticationProviders和它自己的可选的parent AuthenticationProvider。当你这样做时,http.authenticationProvider(...)你正在向属于那个的添加 AuthenticationProviderAuthenticationManagerhttp。通过使用,configure(AuthenticationManagerBuilder auth)您可以配置哪个是属于该特定 的AuthenticationManager父级。AuthenticationManagerHttpSecurity

\n\n

Spring 为该特定的父级提供默认配置AuthenticationManager,但是通过使用configure(AuthenticationManagerBuilder auth)您将拒绝 spring 的配置以支持您的(auth)。

\n