Providing an AuthenticationSuccessHandler
for a RememberMeAuthenticationFilter
breaks the filter chain, therefore I would like to override its onSuccessfulAuthentication
method by providing a custom implementation of RememberMeAuthenticationFilter
. But that seems to be quite complicated or elaborate when using simple Java Config.
Providing an ApplicationEventPublisher
is not a solution if one needs access to HttpServletRequest
or HttpServletResponse
.
I managed to do it, but it looks like a hack - is there a better way?
I've done it this way:
http.rememberMe().addObjectPostProcessor(new ObjectPostProcessor<RememberMeAuthenticationFilter>() {
@Override
public <O extends RememberMeAuthenticationFilter> O postProcess(O object) {
RememberMeAuthenticationFilter newFilter = new RememberMeAuthenticationFilter(
(AuthenticationManager) getByReflection(object, "authenticationManager"),
(RememberMeServices) getByReflection(object, "rememberMeServices")
) {
@Override
protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) {
// business logic
}
};
return (O) newFilter;
}
private <O extends RememberMeAuthenticationFilter> Object getByReflection(O object, String name) {
Field field = ReflectionUtils.findField(object.getClass(), name);
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, object);
}
});