title: How To Inject AuthenticationManager using Java Configuration in a Custom Filter
body: <p>I'm using Spring Security 3.2 and Spring 4.0.1</p>
  
  <p>I'm working on converting an xml config into a Java config. When I annotate <code>AuthenticationManager</code> with <code>@Autowired</code> in my Filter, I'm getting an exception </p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
  </code></pre>
  
  <p>I've tried injecting <code>AuthenticationManagerFactoryBean</code> but that also fails with a similar exception.</p>
  
  <p>Here is the XML configuration I'm working from</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans ...&gt;
      &lt;security:authentication-manager id="authenticationManager"&gt;
          &lt;security:authentication-provider user-service-ref="userDao"&gt;
              &lt;security:password-encoder ref="passwordEncoder"/&gt;
          &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;security:http
              realm="Protected API"
              use-expressions="true"
              auto-config="false"
              create-session="stateless"
              entry-point-ref="unauthorizedEntryPoint"
              authentication-manager-ref="authenticationManager"&gt;
          &lt;security:access-denied-handler ref="accessDeniedHandler"/&gt;
          &lt;security:custom-filter ref="tokenAuthenticationProcessingFilter" position="FORM_LOGIN_FILTER"/&gt;
          &lt;security:custom-filter ref="tokenFilter" position="REMEMBER_ME_FILTER"/&gt;
          &lt;security:intercept-url method="GET" pattern="/rest/news/**" access="hasRole('user')"/&gt;
          &lt;security:intercept-url method="PUT" pattern="/rest/news/**" access="hasRole('admin')"/&gt;
          &lt;security:intercept-url method="POST" pattern="/rest/news/**" access="hasRole('admin')"/&gt;
          &lt;security:intercept-url method="DELETE" pattern="/rest/news/**" access="hasRole('admin')"/&gt;
      &lt;/security:http&gt;
  
      &lt;bean class="com.unsubcentral.security.TokenAuthenticationProcessingFilter"
            id="tokenAuthenticationProcessingFilter"&gt;
          &lt;constructor-arg value="/rest/user/authenticate"/&gt;
          &lt;property name="authenticationManager" ref="authenticationManager"/&gt;
          &lt;property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/&gt;
          &lt;property name="authenticationFailureHandler" ref="authenticationFailureHandler"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is the Java Config I'm attempting</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UserDetailsService userDetailsService;
  
      @Autowired
      private PasswordEncoder passwordEncoder;
  
      @Autowired
      private AuthenticationEntryPoint authenticationEntryPoint;
  
      @Autowired
      private AccessDeniedHandler accessDeniedHandler;
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
                  .userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .sessionManagement()
                      .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                      .and()
                  .exceptionHandling()
                      .authenticationEntryPoint(authenticationEntryPoint)
                      .accessDeniedHandler(accessDeniedHandler)
                      .and();
          //TODO: Custom Filters
      }
  }
  </code></pre>
  
  <p>And this is the Custom Filter class. The line giving me trouble is the setter for AuthenticationManager</p>
  
  <pre><code>@Component
  public class TokenAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {
  
  
      @Autowired
      public TokenAuthenticationProcessingFilter(@Value("/rest/useAuthenticationManagerr/authenticate") String defaultFilterProcessesUrl) {
          super(defaultFilterProcessesUrl);
      }
  
  
      @Override
      public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        ...
      }
  
      private String obtainPassword(HttpServletRequest request) {
          return request.getParameter("password");
      }
  
      private String obtainUsername(HttpServletRequest request) {
          return request.getParameter("username");
      }
  
      @Autowired
      @Override
      public void setAuthenticationManager(AuthenticationManager authenticationManager) {
          super.setAuthenticationManager(authenticationManager);
      }
  
      @Autowired
      @Override
      public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
          super.setAuthenticationSuccessHandler(successHandler);
      }
  
      @Autowired
      @Override
      public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
          super.setAuthenticationFailureHandler(failureHandler);
      }
  }
  </code></pre>