title: How to forward User back to desired protected page after successful login with Spring Security
body: <p>I'm using Spring Security to handle all of the authentication of my web app.  Here's the situation, let's say I have three JSP pages:</p>
  
  <p>1) Login Page<br/>
  2) Default Home Page (needs authentication)<br/>
  3) user account page (needs authentication)</p>
  
  <p>The standard flow works just fine:</p>
  
  <p>1) User successfully logs in from Login Page<br/>
  2) User is taken to default homepage</p>
  
  <p><strong>Here's the use-case I cannot figure out</strong></p>
  
  <p>1) User attempts to access their account page (but it is password protected) - works<br/>
  2) User is redirected to enter their credentials - works<br/>
  3) User successfully gives credentials - works<br/>
  4) User is redirected to their account page - DOESN'T WORK (it redirects them to the default homepage)</p>
  
  <p>I'm using a custom <code>AuthenticationSuccessHandler</code>, but the trouble is, whenever I look to the request to see where they came from it's always from <code>/j_spring_security_check</code>.</p>
  
  <p>Is there some simple way of isolating what page they came from?</p>
  
  <p>Here's what's in my applicationContext.xml that pertains to my Spring Security setup:</p>
  
  <pre><code>&lt;bean id="successfulLoginService" class="com.service.SuccessfulLoginService"&gt;
      &lt;property name="defaultTargetUrl" value="/listings/add_listing.htm"/&gt;
    &lt;/bean&gt;
  
    &lt;security:http pattern="/**.html" security="none" /&gt;
    &lt;security:http pattern="/login/*.htm" security="none" /&gt;
    &lt;security:http pattern="/listings/listing.htm" security="none" /&gt;
    &lt;security:http pattern="/listings/viewListing.htm" security="none" /&gt;
    &lt;security:http pattern="/search/keyword/*.htm" security="none" /&gt;
  
    &lt;security:http auto-config="true"&gt;
      &lt;security:intercept-url pattern="/**.htm" access="ROLE_USER" /&gt;
      &lt;security:intercept-url pattern="/**/**.htm" access="ROLE_USER" /&gt;
      &lt;security:form-login login-page="/login/login.htm" authentication-failure-handler-ref="failedLoginService" authentication-success-handler-ref="successfulLoginService"/&gt;
    &lt;/security:http&gt;
  
    &lt;security:authentication-manager&gt;
      &lt;security:authentication-provider
        user-service-ref="userDetailsService" /&gt;
    &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p>Here's my SuccessfulLoginService.java:</p>
  
  <pre><code>@Service("successfulLoginService")
  public class SuccessfulLoginService extends SimpleUrlAuthenticationSuccessHandler
  {
    @Autowired
    UserDao userDao;
  
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException
    {
      Users user = null;
      String username = ((SpringSecurityUser) authentication.getPrincipal()).getUsername();
      try
      {
        user = userDao.getUserByEmail(username);
      } catch (Exception e)
      {
        e.printStackTrace();
        throw new ServletException("Failed to login", e);
      }
      request.getSession().setAttribute("user", user);
  
      response.sendRedirect("/MyApplication" + determineTargetUrl(request, response));
    }
  }
  </code></pre>