Search code examples
javaspring-security

In memory and custom providers all together


I'm setting up my Spring Security (v4.0.1) web application. I want to have two authentication providers, an "in-memory" one to manage the administrator account and a custom one which refers to my own implementation. The system should attempt the authentication against the "in-memory" provider first of all and against the custom one in second place. My code looks like this:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, 
    AuthenticationProvider provider) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("admin")
            .password("s3cr3t")
            .authorities("ADMIN");
    auth.authenticationProvider(provider);
}

However, this code leads the framework to try my custom implementation first. It makes a bit of sense, since the AuthenticationManagerBuilder#authenticationProvider method adds a Provider to the internal List while the AuthenticationManagerBuilder#inMemoryAuthentication one configures it internally. How could I manage to get it work?


Solution

  • You can create your InMemoryUserDetailsManagerConfigurer manually and tell it to configure itself on the AuthenticationManagerBuilder when you have finished configuring it so it installs it's AuthenticationProvider before your custom one:

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth,
            AuthenticationProvider provider) throws Exception {
    
        inMemoryConfigurer()
            .withUser("admin")
                .password("s3cr3t")
                .authorities("ADMIN")
            .and()
            .configure(auth);
        auth.authenticationProvider(provider);
    }
    
    private InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder>
            inMemoryConfigurer() {
        return new InMemoryUserDetailsManagerConfigurer<>();
    }
    

    Normally what happens is that the InMemoryUserDetailsManagerConfigurer is created and added to the list of configurers that should be applied when the AuthenticationManager is built - which is after you've installed your custom AuthenticationProvider.