带有java配置的{spring} AuthenticationFailureHandler

时间:2015-09-07 22:02:55

标签: java spring spring-mvc spring-security

我创建了一个自定义身份验证失败处理程序,如下所示:

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");
            }
        }
    }

我创建了一个像这样的bean:

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

和spring配置如下:

    @Autowired
    private TrackerAuthFailureHandler trackerAuthFailureHandler;

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

但是没有发现bean的异常。任何想法?

1 个答案:

答案 0 :(得分:4)

使用注释注入方式时,必须在TrackerAuthFailureHandler类中使用@Component。像这样:。

@Component
public class TrackerAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler{