Spring boot 如何在SpringBootJavaConfig中进行可插拔身份验证?

Spring boot 如何在SpringBootJavaConfig中进行可插拔身份验证?,spring-boot,authentication,plugins,Spring Boot,Authentication,Plugins,我正在将单片web应用程序现代化迁移到Spring Boot微型项目中 旧应用程序的最大特点之一是身份验证插件模型,所有这些都基于XML配置。通过打开和关闭指定的Spring配置文件,我可以启用某些登录方式 我主要有: 以“root”身份登录,密码在配置中加密(是的,默认密码是众所周知的“root”) 用户名和密码与数据库中存储的哈希值进行比较 活动目录 通用LDAP 预授权标头SSO(即SiteMinder和兼容) OAuth2通过Office 365 X509 SAML2.0 多个特定于客

我正在将单片web应用程序现代化迁移到Spring Boot微型项目中

旧应用程序的最大特点之一是身份验证插件模型,所有这些都基于XML配置。通过打开和关闭指定的Spring配置文件,我可以启用某些登录方式

我主要有:

  • 以“root”身份登录,密码在配置中加密(是的,默认密码是众所周知的“root”)
  • 用户名和密码与数据库中存储的哈希值进行比较
  • 活动目录
  • 通用LDAP
  • 预授权标头SSO(即SiteMinder和兼容)
  • OAuth2通过Office 365
  • X509
  • SAML2.0
  • 多个特定于客户的插件,每个插件都有自己的配置文件
每个插件都有自己的XML配置文件,由各自的配置文件保护

现在我做了以下工作:

@Order(90) //Lowest, so it is the first
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http//

            .headers(httpSecurityHeadersConfigurer -> httpSecurityHeadersConfigurer
                //<sec:xss-protection/>
                .xssProtection(xXssConfig -> xXssConfig.block(true))
             .... and so on

            .formLogin(formLogin -> .....)
    }


    //Generic configuration of the AuthenticationManagerBuilder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.eraseCredentials(true);
        super.configure(auth);
    }

}

@Order(101)
@Profile(SpringProfiles.ROOT_USER)
@Configuration
public class RootUserSecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        log.warn("Initializing authentication for security profile {}", getBeanName());
        auth.authenticationProvider(rootAuthenticationProvider());
    }

}

@Order(102)
@Profile(SpringProfiles.DATABASEUSER)
@Configuration
public class DbUserSecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        log.warn("Initializing authentication for security profile {}", getBeanName());
        auth.authenticationProvider(dbAuthenticationProvider());
    }

}
我的理解是,
UsernamePasswordAuthenticationFilter
没有绑定到任何提供者,它应该绑定到提供两个提供者的
ProviderManager
,一个用于root用户,另一个用于数据库用户

请注意,当我一件一件地开发时,我选择只移植系统帐户和数据库登录,并在接下来的编码日中留下额外的插件

问题:为什么使用附加的
WebSecurityConfigurationAdapter
类无法将新添加的
AuthenticationProvider
正确注册到上下文中


编辑

我也试过:

  • 接下来,我尝试将我的插件更改为
    @Autowired
    方法注入
    AuthenticationManagerBuilder
    ,但没有成功

解决方案是从
globalaauthenticationconfigureradapter
扩展,而不是
WebSecurityConfigurerAdapter

这是因为WebSecurityConfigureAdapter是一种生成安全筛选器的“全局”配置。在内部,它为
AuthenticationManager
生成一个配置器对象,即
AuthenticationManagerBuilder

为了让这个实例在profiles bean之间流动,Spring寻找实现
securityconfigure
的其他bean,其中
globalaauthenticationconfigureradapter
是一个方便的抽象类

此方法仅用于连接到用户名/密码身份验证的身份验证提供程序

需要额外过滤器的SSO插件应该以不同的方式声明。我将在接下来的几天工作

org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:227) ~[spring-security-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:95) ~[spring-security-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)