lch
lch

Reputation: 4931

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?

Upvotes: 3

Views: 4518

Answers (1)

reza ramezani matin
reza ramezani matin

Reputation: 1474

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

@Component
public class TrackerAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler{

Upvotes: 4

Related Questions