简体   繁体   中英

spring security AuthenticationFailureHandler with java config

i created a custom authentication failure handler like this:

public class TrackerAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler{

        @Override
        public void onAuthenticationFailure(HttpServletRequest request,
                HttpServletResponse response, AuthenticationException exception)
                throws IOException, ServletException {
            super.onAuthenticationFailure(request, response, exception);

            if(exception.getClass().isAssignableFrom(DisabledException.class)){
                setDefaultFailureUrl("/accountRecovery");
            }
        }
    }

and i created a bean like this:

@Bean
    public AuthenticationFailureHandler trackerAuthFailureHandler(){
        SimpleUrlAuthenticationFailureHandler handler=new SimpleUrlAuthenticationFailureHandler();
        return handler;
    }

and spring config like this:

    @Autowired
    private TrackerAuthFailureHandler trackerAuthFailureHandler;

    @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .failureHandler(trackerAuthFailureHandler);
    }

But bean not found exception occurred . any ideas?

When you use annotation injection way then you must use @Component in top of class TrackerAuthFailureHandler. like this:.

@Component
public class TrackerAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM