Comment injecter AuthenticationManager à l’aide de la configuration Java dans un filtre personnalisé

J’utilise Spring Security 3.2 et Spring 4.0.1

Je travaille sur la conversion d’une configuration xml en une configuration Java. Lorsque @Autowired AuthenticationManager avec @Autowired dans mon filtre, je reçois une exception

 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: {} 

J’ai essayé d’injecter AuthenticationManagerFactoryBean mais cela échoue également avec une exception similaire.

Voici la configuration XML sur laquelle je travaille

                        

Voici la configuration Java que je tente

 @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 } } 

Et c’est la classe de filtre personnalisé. La ligne qui me pose problème est le setter pour AuthenticationManager

 @Component public class TokenAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter { @Autowired public TokenAuthenticationProcessingFilter(@Value("/rest/useAuthenticationManagerr/authenticate") Ssortingng defaultFilterProcessesUrl) { super(defaultFilterProcessesUrl); } @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { ... } private Ssortingng obtainPassword(HttpServletRequest request) { return request.getParameter("password"); } private Ssortingng 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); } } 

Remplacez la méthode authenticationManagerBean dans WebSecurityConfigurerAdapter pour exposer AuthenticationManager à l’aide de configure(AuthenticationManagerBuilder) tant que bean Spring:

Par exemple:

  @Bean(name = BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }