title: IllegalStateException: Cannot apply ... to already built object
body: <p>I am using spring-boot in version 1.5.6.RELEASE</p>
  
  <p>I'm getting following exception.</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer@6b5c38ed to already built object
      at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.add(AbstractConfiguredSecurityBuilder.java:196)
      at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.apply(AbstractConfiguredSecurityBuilder.java:133)
      at org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder.apply(AuthenticationManagerBuilder.java:290)
      at org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder.inMemoryAuthentication(AuthenticationManagerBuilder.java:134)
      at com.mca.security.configurer.auth.FirstAuthenticationManagerConfigurer.configure(FirstAuthenticationManagerConfigurer.java:12)
      at com.mca.security.configurer.auth.FirstAuthenticationManagerConfigurer.configure(FirstAuthenticationManagerConfigurer.java:7)
      at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.configure(AbstractConfiguredSecurityBuilder.java:384)
      at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:330)
  </code></pre>
  
  <p>I've managed to reproduce it in here:</p>
  
  <p><a href="https://github.com/mironbalcerzak/spring-boot-security-issue" rel="nofollow noreferrer">https://github.com/mironbalcerzak/spring-boot-security-issue</a></p>
  
  <p>Idea behind the implementation is very simple.
  I wish to create few SecurityConfigurer's (both for HttpSecurity and AuthenticationManagerBuilder) and combine them using apply() in class extending WebSecurityAdapter.</p>
  
  <p>Correct me if I am wrong and that's not the way to do it.</p>
  
  <p>Workaround I found is extracting "apply" method to SecurityConfigurerAdapter.init(...), and in SecurityConfigurerAdapter.configure(...) operating on already created object. </p>
  
  <p>Here's the workaround (that makes me very unhappy :) )</p>
  
  <pre><code>public class FirstAuthenticationManagerConfigurer
          extends SecurityConfigurerAdapter&lt;AuthenticationManager, AuthenticationManagerBuilder&gt; {
  
      private DaoAuthenticationConfigurer&lt;AuthenticationManagerBuilder, UserDetailsManager&gt; configurer;
  
      @Override
      public void init(AuthenticationManagerBuilder builder) throws Exception {
          InMemoryUserDetailsManagerConfigurer&lt;AuthenticationManagerBuilder&gt; managerConfigurer = builder.inMemoryAuthentication();
          managerConfigurer.withUser("user1").password("password").roles("SYSTEM");
          UserDetailsManager userDetailsService = managerConfigurer.getUserDetailsService();
          configurer = builder.userDetailsService(userDetailsService);
      }
  
      @Override
      public void configure(AuthenticationManagerBuilder builder) throws Exception {
          configurer.configure(builder);
      }
  }
  </code></pre>
  
  <p>...have a good day guys and happy coding :)</p>