spring-projects / spring-projects/spring-security

SEC-2961: Configuring AuthenticationManagerBuilder with GlobalAuthenticationConfigurerAdapter fails

Open
#3,089 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug type: jira
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Yves Alter (Migrated from SEC-2961) said:

I already posted this issue on stackoverflow some time ago, but I thought I better file this as a bug here, too.

According to the documentation of the SecurityConfigurer interface, the implementation of init(SecurityBuilder) should NOT set properties on the passed SecurityBuilder object. Instead this should be done in the configure(SecurityBuilder) method. So I tried the following implementation:

@Configuration
  protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

  @Autowired
  private WebUserDetailsService userDetailsService;

  @Autowired
  private WebUserPasswordEncoder passwordEncoder;

  @Override
  public void configure (AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(this.userDetailsService).passwordEncoder(this.passwordEncoder);
  }
}

This configuration fails during startup due to the following check in the frameworks AbstractConfiguredSecurityBuilder:

 if(buildState.isConfigured()) {
   throw new IllegalStateException("Cannot apply "+configurer+" to already built object");
}

BuildState.isConfigured() has the following (suprising) implementation:

public boolean isConfigured() {
  return order >= CONFIGURING.order;
}

It actually checks, if the build is currently in the CONFIGURING phase (as stated in javadoc), but not if it already is configured (that would be the BUILT state, I guess), as the method name suggests.

So my question is: Is this expected behavior or is it a bug in the Java Configuration? All other examples I find on the web usually configure the builder in the init() method, so maybe I just don't understand the documentation correctly?

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 AbstractConfiguredSecurityBuilder and inspect BuildState.isConfigured(), then compare that lifecycle check with the SecurityConfigurer init(SecurityBuilder) and configure(SecurityBuilder) contract described in the issue. Done means the documented GlobalAuthenticationConfigurerAdapter configuration has a clear, verified outcome: either it works as documented or the expected behavior is clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.