What is the equivalent Java configuration for the Spring Security tag?
A few issues you may need to keep in mind:
Your filter needs to be added before the standard UsernamePasswordAuthenticationFilter
http.addFilterBefore(customUsernamePasswordAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class)
If you extend UsernamePasswordAuthenticationFilter your filter will return immediately without doing anything unless you set a RequestMatcher
myAuthFilter.setRequiresAuthenticationRequestMatcher(
new AntPathRequestMatcher("/login","POST"));
All the configuration you do in http.formLogin().x().y().z() is applied to the standard UsernamePasswordAuthenticationFilter not the custom filter you build. You will need to configure it manually yourself. My auth filter initialization looks like this:
@Bean
public MyAuthenticationFilter authenticationFilter() {
MyAuthenticationFilter authFilter = new MyAuthenticationFilter();
authFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POST"));
authFilter.setAuthenticationManager(authenticationManager);
authFilter.setAuthenticationSuccessHandler(new MySuccessHandler("/app"));
authFilter.setAuthenticationFailureHandler(new MyFailureHandler("/login?error=1"));
authFilter.setUsernameParameter("username");
authFilter.setPasswordParameter("password");
return authFilter;
}