Using multiple WebSecurityConfigurerAdapter with different AuthenticationProviders (basic auth for API and LDAP for web app)

后端 未结 1 1470
长发绾君心
长发绾君心 2020-12-05 05:21

According the Spring Security Reference section 5.7 it should be possible to define more than one security adapter.

I try to do the same but without success. After a

相关标签:
1条回答
  • 2020-12-05 05:56

    You use the same AuthenticationManager for both configurations, because you autowire the same AuthenticationManagerBuilder.

    See Spring Security Architecture:

    @Configuration
    public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    
        ... // web stuff here
    
        @Autowired
        public void initialize(AuthenticationManagerBuilder builder, DataSource dataSource) {
            builder.jdbcAuthentication().dataSource(dataSource).withUser("dave")
                .password("secret").roles("USER");
        }
    
    }
    

    This example relates to a web application, but the usage of AuthenticationManagerBuilder is more widely applicable (see below for more detail on how web application security is implemented). Note that the AuthenticationManagerBuilder is @Autowired into a method in a @Bean - that is what makes it build the global (parent) AuthenticationManager. In contrast if we had done it this way:

    @Configuration
    public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    
        @Autowired
        DataSource dataSource;
    
        ... // web stuff here
    
        @Override
        public void configure(AuthenticationManagerBuilder builder) {
            builder.jdbcAuthentication().dataSource(dataSource).withUser("dave")
                .password("secret").roles("USER");
        }
    
    }
    

    (using an @Override of a method in the configurer) then the AuthenticationManagerBuilder is only used to build a "local" AuthenticationManager, which is a child of the global one.

    0 讨论(0)
提交回复
热议问题