title: Spring Security authenticationSuccessHandler for RememberMeAuthenticationFilter
body: <p>I want to implement a custom <code>AuthenticationSuccessHandler</code> for my login filter which is <code>org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter</code>. </p>
  
  <p>Here is my spring security configuration</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans 
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:security="http://www.springframework.org/schema/security"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt;
  
      &lt;security:http entry-point-ref="restAuthenticationEntryPoint" disable-url-rewriting = "true" auto-config="true" use-expressions="true"&gt;
          &lt;security:intercept-url pattern="/api/*" access="hasRole('AUTHENTICATED_USER')"/&gt;
          &lt;security:remember-me key="spring_login_detail" services-ref="rememberMeServices"/&gt;
          &lt;security:form-login login-processing-url="/login"/&gt;
  
          &lt;security:logout
              invalidate-session="true"
              delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE"
              logout-url="/logout" 
          /&gt;
      &lt;/security:http&gt;
  
      &lt;security:global-method-security secured-annotations="enabled"  pre-post-annotations="enabled"/&gt;
  
      &lt;security:authentication-manager alias="authenticationManager"&gt;
          &lt;security:authentication-provider ref="rememberMeAuthenticationProvider"/&gt;
          &lt;security:authentication-provider user-service-ref="customUserDetailsService"&gt;
              &lt;security:password-encoder ref="passwordEncoder"/&gt;
          &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/&gt;
  
      &lt;bean id="mySuccessHandler" class="com.projectname.security.CustomSavedRequestAwareAuthenticationSuccessHandler"/&gt;
  
      &lt;bean id="customUserDetailsService" class="com.projectname.security.CustomUserDetailsService"/&gt;
  
      &lt;bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices"&gt;
          &lt;property name="key" value="jsfspring-sec" /&gt;
          &lt;property name="userDetailsService" ref="customUserDetailsService" /&gt;
          &lt;property name="alwaysRemember" value="false" /&gt;
          &lt;property name="tokenValiditySeconds" value="1209600" /&gt;
          &lt;property name="parameter" value="_spring_security_remember_me_input"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="rememberMeAuthenticationProvider" class="org.springframework.security.authentication.RememberMeAuthenticationProvider"&gt;
          &lt;property name="key" value="spring_login_detail"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="rememberMeFilter" class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter"&gt;
          &lt;property name="rememberMeServices" ref="rememberMeServices"/&gt;
          &lt;property name="authenticationManager" ref="authenticationManager" /&gt;
          &lt;property name="authenticationSuccessHandler" ref="mySuccessHandler"/&gt;
      &lt;/bean&gt; 
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here's my custom <code>AuthenticationSuccessHandler</code> implementation</p>
  
  <pre><code>import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
  import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
  import org.springframework.security.web.savedrequest.RequestCache;
  import org.springframework.security.web.savedrequest.SavedRequest;
  import org.springframework.util.StringUtils;
  
  public class CustomSavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
  
      private RequestCache requestCache = new HttpSessionRequestCache();
  
      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
              Authentication authentication) throws ServletException, IOException {
          SavedRequest savedRequest = requestCache.getRequest(request, response);
          if (savedRequest == null) {
              clearAuthenticationAttributes(request);
              return;
          }
  
          String targetUrlParam = getTargetUrlParameter();
          if (isAlwaysUseDefaultTargetUrl()
                  || (targetUrlParam != null
                  &amp;&amp; StringUtils.hasText(request.getParameter(targetUrlParam)))) {
              requestCache.removeRequest(request, response);
              clearAuthenticationAttributes(request);
              return;
          }
  
          clearAuthenticationAttributes(request);
      }
  
      public void setRequestCache(RequestCache requestCache) {
          this.requestCache = requestCache;
      }
  }
  </code></pre>
  
  <p>The problem is after a successful authentication the <code>onAuthenticationSuccess</code> isn't called at all. I've read an answer on StackOverflow that says that I need to implement <code>onAuthenticationSuccess</code> instead of <code>SimpleUrlAuthenticationSuccessHandler</code>. I've tried to do that, still didn't work. Everything else is fine, the only problem is each time I log in, spring just redirected me to <code>'/'</code>. Which is not what I want, I want it just to return <code>'200 OK'</code></p>