title: Spring Security redirect to previous page after successful login
body: <p>I know this question has been asked before, however I'm facing a particular issue here.</p>
  
  <p>I use spring security 3.1.3.</p>
  
  <p>I have 3 possible login cases in my web application:</p>
  
  <ol>
  <li>Login via the login page : OK.</li>
  <li>Login via a restricted page : OK too.</li>
  <li>Login via a non-restricted page : not OK... a "product" page can be accessed by everybody, and a user can post a comment if he's logged. So a login form is contained in the same page in order to allow users to connect.</li>
  </ol>
  
  <p>The problem with case 3) is that I can't manage to redirect users to the "product" page. They get redirected to the home page after a successful login, no matter what.</p>
  
  <p>Notice that with case 2) the redirection to the restricted page works out of the box after successful login.</p>
  
  <p>Here's the relevant part of my security.xml file:</p>
  
  <pre><code>&lt;!-- Authentication policy for the restricted page  --&gt;
  &lt;http use-expressions="true" auto-config="true" pattern="/restrictedPage/**"&gt;
      &lt;form-login login-page="/login/restrictedLogin" authentication-failure-handler-ref="authenticationFailureHandler" /&gt;
      &lt;intercept-url pattern="/**" access="isAuthenticated()" /&gt;
  &lt;/http&gt;
  
  &lt;!-- Authentication policy for every page --&gt;
  &lt;http use-expressions="true" auto-config="true"&gt;
      &lt;form-login login-page="/login" authentication-failure-handler-ref="authenticationFailureHandler" /&gt;
      &lt;logout logout-url="/logout" logout-success-url="/" /&gt;
  &lt;/http&gt;
  </code></pre>
  
  <p>I suspect the "authentication policy for every page" to be responsible for the problem. However, if I remove it I can't login anymore... j_spring_security_check sends a 404 error.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Thanks to Ralph, I was able to find a solution. So here's the thing: I used the property  </p>
  
  <pre><code>&lt;property name="useReferer" value="true"/&gt;
  </code></pre>
  
  <p>that Ralph showed me. After that I had a problem with my case 1) : when logging via the login page, the user stayed in the same page (and not redirected to the home page, like it used to be). The code until this stage was the following:</p>
  
  <pre><code>&lt;!-- Authentication policy for login page --&gt;
  &lt;http use-expressions="true" auto-config="true" pattern="/login/**"&gt;
      &lt;form-login login-page="/login" authentication-success-handler-ref="authenticationSuccessHandlerWithoutReferer" /&gt;
  &lt;/http&gt;
  
  &lt;!-- Authentication policy for every page --&gt;
  &lt;http use-expressions="true" auto-config="true"&gt;
      &lt;form-login login-page="/login" authentication-failure-handler-ref="authenticationFailureHandler" /&gt;
      &lt;logout logout-url="/logout" logout-success-url="/" authentication-success-handler-ref="authenticationSuccessHandler"/&gt;
  &lt;/http&gt;
  
  &lt;beans:bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt;
      &lt;!-- After login, return to the last visited page --&gt;
      &lt;beans:property name="useReferer" value="true" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="authenticationSuccessHandlerWithoutReferer" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt;
      &lt;!-- After login, stay to the same page --&gt;
      &lt;beans:property name="useReferer" value="false" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p>This should work, in theory at least, but it wasn't. I still dont know why, so if someone has an answer on this, I will gladly create a new topic to allo him to share his solution.</p>
  
  <p>In the meantime, I came to a workaround. Not the best solution, but like I said, if someone has something better to show, I'm all ears. So this is the new authentication policy for the login page :</p>
  
  <pre><code>&lt;http use-expressions="true" auto-config="true" pattern="/login/**" &gt;
      &lt;intercept-url pattern="/**" access="isAnonymous()" /&gt;
      &lt;access-denied-handler error-page="/"/&gt;
  &lt;/http&gt;
  </code></pre>
  
  <p>The solution here is pretty obvious: the login page is allowed only for anonymous users. Once a user is connected, the error handler redirects him to the home page.</p>
  
  <p>I did some tests, and everything seems to be working nicely.</p>