spring-projects / spring-projects/spring-security

DaoAuthenticationProvider is autoconfigured when more than one AuthenticationProvider is registered

Open
#10,005 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: config status: feedback-provided type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Describe the bug
By default, the AuthenticationManagerBuilderis autoconfigured with an AuthenticationProvider, if registered, or with a DaoAuthenticationProvider, if an UserDetailsService is registered. Both configurer back off if the AuthenticationManagerBuilder is already configured, i.e. if at least one AuthenticationProvider has been specified.

In particular, if a user registers their own AuthenticationProvider bean, it is added to the manager's providers list by InitializeAuthenticationProviderManagerConfigurer, and InitializeUserDetailsManagerConfigurer does nothing since it consider the manager already configured.

However, InitializeAuthenticationProviderManagerConfigurer only add the provider if there's just a single registered one. If multiple beans are registered, the configurer does nothing, but because the manager's providers list is still empty InitializeUserDetailsManagerConfigurer thinks that the manager was not yet configured, and creates the DaoAuthenticationProvider.

This is a problem if I want both a DaoAuthenticationProvider to handle UsernamePasswordAuthenticationToken and a second provider to handle SSO tokens. If I only register the second provider, SSO works but the DaoAuthenticationProvider is not present in the ProviderManager's providers list, so I cannot log in with username/password. If I also register a DaoAuthenticationProvider, the default one also will be created, possibly with a different configuration (e.g. a different PasswordEncoder).

The workaround I found is to register my SSO provider:

@Bean
public AuthenticationProvider ssoProvider(AuthenticationManagerBuilder auth) {
    var provider = new MySsoProvider();
    auth.authenticationProvider(provider);
    return provider;
}

and a dummy one:

@Bean
public AuthenticationProvider dummyAuthProvider() {
    return new AuthenticationProvider() {
        public boolean supports(Class<?> authentication) {
            return false;
        }
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            return null;
        }
    };
}

so I can use my SSO provider and the Dao provider configured by InitializeUserDetailsManagerConfigurer, but this is clearly not the intended way to do it.

To Reproduce
Register a UserDetailsService and multiple AuthenticationProviders in a @Configuration.

Expected behavior
InitializeUserDetailsManagerConfigurer should behave the same if either one or more than one AuthenticationProvider beans are registered.

Sample

spring-security-dao-configurer

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with AuthenticationConfiguration.java, InitializeAuthenticationProviderBeanManagerConfigurer.java, InitializeUserDetailsBeanManagerConfigurer.java, and AuthenticationManagerBuilder.java at the linked locations. Reproduce the setup from the spring-security-dao-configurer sample with one UserDetailsService and multiple AuthenticationProvider beans; done means the intended providers are present without an unintended DaoAuthenticationProvider.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.