title: Spring Security : configure(AuthenticationManagerBuilder auth) vs authenticationManagerBean()
body: <p>I am configuring Spring Security. To authenticate and authorize users, I override <code>configure(AuthenticationManagerBuilder auth)</code> of <code>WebSecurityConfigurerAdapter</code>. This works fine. Below is my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth
          .userDetailsService(customUserDetailsService)
          .passwordEncoder(getPasswordEncoder());
  }
  </code></pre>
  
  <p>But when I try to to enable method level security, per action, using      <code>@EnableGlobalMethodSecurity(securedEnabled = true)</code> it throws an exception:</p>
  
  <blockquote>
    <p>No AuthenticationManager found</p>
  </blockquote>
  
  <p>As per my understanding <code>AuthenticationManager</code> is used to authenticate and authorize users, which I was already doing using <code>configure(AuthenticationManagerBuilder auth)</code> and Spring was injecting <code>auth</code> object itself.</p>
  
  <p>Why I need to register <code>AuthenticationManager</code> manually?</p>
  
  <pre class="lang-java prettyprint-override"><code>@Bean @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
      return super.authenticationManagerBean();
  }
  </code></pre>
  
  <p>What are the differennt purposes  <code>configure(AuthenticationManagerBuilder auth)</code> and <code>authenticationManagerBean()</code> serves?</p>
  
  <p>I am extending <code>WebSecurityConfigurerAdapter</code>. Why I need to provide a custom <code>AuthenticationManager</code> by overriding <code>authenticationManagerBean()</code>.</p>