Spring boot Spring安全性:具有多个AuthenticationManager的多个http元素

Spring boot Spring安全性:具有多个AuthenticationManager的多个http元素,spring-boot,spring-security,Spring Boot,Spring Security,我正在为Spring安全性的Java配置而挣扎。我有多个入口点,但无法正确设置AuthenticationManagers 我的第一个配置文件如下所示: @Configuration @EnableWebSecurity @Order(100) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http

我正在为Spring安全性的Java配置而挣扎。我有多个入口点,但无法正确设置
AuthenticationManager
s

我的第一个配置文件如下所示:

@Configuration
@EnableWebSecurity
@Order(100)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
            .antMatcher("/service/**")
            .addFilterAfter(requestHeaderAuthenticationFilter(), SecurityContextPersistenceFilter.class)
                .authorizeRequests()
                .antMatchers("/service/**").authenticated()
            .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .and()
                .csrf().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.authenticationProvider(preAuthenticatedAuthenticationProvider(null));
    }

@Bean
public RequestHeaderAuthenticationFilter requestHeaderAuthenticationFilter() throws Exception
{
    // Takes the value of the specified header as the user principal
    RequestHeaderAuthenticationFilter filter = new RequestHeaderAuthenticationFilter();
    filter.setPrincipalRequestHeader("SECRET_HEADER");
    filter.setAuthenticationManager(authenticationManager());
    filter.setExceptionIfHeaderMissing(false);
    return filter;
}
这一切都正常工作。当我在
RequestHeaderAuthenticationFilter
中设置断点时,我看到一个
AuthenticationManager
,其中有一个
AuthenticationProvider
,即
PreAuthenticationDauthenticationProvider
(未显示,因为它只是一个普通的旧bean)

我还为管理员用户等提供了一个特殊的安全链:

@Configuration
@Order(101)
public class AdminSecurity extends WebSecurityConfigurerAdapter
{

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
            .authenticationProvider(mainSiteLoginAuthenticationProvider())
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .antMatchers("/admin/**").access("SECRET ADMIN ACCESS EXPRESSION")
                .antMatchers("/internal/**").access("SECRET INTERNAL ACCESS EXPRESSION")
                .anyRequest().permitAll()
                .and()
            .formLogin()
                .defaultSuccessUrl("/admin/thing")
                .loginPage("/login")
                .loginProcessingUrl("/do_login")
                .defaultSuccessUrl("/admin/thing")
                .failureUrl("/login?error=true")
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
            .logout()
                .and()
            .exceptionHandling()
                //.authenticationEntryPoint(null) // entry-point-ref="loginEntryPoint"
                .and()
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)  // create-session="ifRequired"
                .and()
            .csrf().disable();
    }
这现在可以正常工作了(经过很多努力),但是如果我在
UsernamePasswordAuthenticationFilter
中设置一个断点,我会看到这个筛选器有一个不同的
AuthenticationManager
实例,它是按预期由
mainSiteLoginAuthenticationProvider
提供的。但是,它有一个父级
AuthenticationManager
,该父级由默认的
DaoAuthenticationProvider
配置,该默认值在日志中生成临时密码:

Using default security password: 47032daf-813e-4da1-a224-b6014a705805
因此,我的问题是:

  • 如何使两个安全配置使用相同的
    AuthenticationManager
    ?我认为作为订单100的
    SecurityConfig
    将创建一个,然后作为101的
    AdminConfig
    将使用它。但我无法让他们使用相同的
    AuthenticationManager
  • 否则,如何防止
    AdminConfig
    AuthenticationManager
    生成具有默认
    DAOAAuthenticationProvider
    的父级

我使用的是Spring Boot 1.5.9.RELEASE,这意味着Spring Security 4.2.3.RELEASE。

您首先配置的是构建一个本地
AuthenticationManager
。您必须公开它才能在不同的配置中使用它。您的第二个配置没有生成任何身份验证管理器,因此它使用全局身份验证管理器。如何公开本地身份验证管理器?如何让其他配置使用它?