Spring SecurityでAuthenticationSuccessHandlerが動かない件

さのです。意外とブログ続いてて自分でもびっくりです。
今日はSpring Frameworkシリーズ第2弾。
Spring SecurityでAuthnticationSuccessHandlerが動かない件について。

ログイン成功時になんやかんやの処理をしたくてAuthenticationSuccessHandlerを追加したんですが、動きませんでした。

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    SuccessHandler successHandler;
    @Autowired
    FailureHandler failureHandler;
    
    @Bean
    SuccessHandler successHander() {
        return new SuccessHandler();
    }
    @Bean
    FailureHandler failureHandler() {
        return new FailureHandler();
    }

    @Override
    public void configure(WebSecurity web) throws Exception{
        web.ignoring().antMatchers("/js/**", "/css/**","/img/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
            .antMatchers("/loginForm").permitAll()
            .anyRequest().authenticated();

        http.formLogin()
            .loginProcessingUrl("/login")
            .loginPage("/loginForm")
            .successHandler(successHandler)
            .failureHandler(failureHandler)
            .defaultSuccessUrl("contents/index")
            .usernameParameter("mailaddress").passwordParameter("password")
            .and();

        http.logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout**"))
            .logoutSuccessUrl("/loginForm");
    }

    public class SuccessHandler implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res, Authentication auth) throws IOException, ServletException {
            // ログイン成功時のなんやかんやの処理
        }
    }
    
    public class FailureHandler implements AuthenticationFailureHandler {
        @Override
     public void onAuthenticationFailure(HttpServletRequest req, HttpServletResponse res, AuthenticationException ex)throws IOException, ServletException {
            // ログイン失敗時のなんやかんやの処理
        }
    }
}

ログイン失敗時のAuthenticationFailureHandlerは動いているので書き方は間違っていないはず。
ということはなんかの処理と競合しててそっちが呼ばれてるのかなという読み。
怪しいのはdefaultSuccessUrlあたりでしょうか。 コメント化してみました。

       http.formLogin()
            .loginProcessingUrl("/login")
            .loginPage("/loginForm")
            .successHandler(successHandler)
            .failureHandler(failureHandler)
//         .defaultSuccessUrl("contents/index")
            .usernameParameter("mailaddress").passwordParameter("password")
            .and();

どうやら正解だったようでSuccessHandllerが呼ばれるようになりました。
しかし、今度はログイン後にindexに飛ばなくなるので、SuccessHandlerからindexにリダイレクトするようにします。

   public class SuccessHandler implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res, Authentication auth) throws IOException, ServletException {
            // ログイン成功時のなんやかんやの処理

       // リダイレクト先を設定
       res.redirect("contents/index.html", false);
        }
    }

これでindexに飛ぶようになりました。 めでたしめでたし。