title: Spring Security redirect back to refer URL after login and logout
body: <p>I am using Spring Security for authentication. The login and logout buttons are placed on the page layout, which is used by all pages. I have a BookController which needs a parameter for the create action (e.g., an image id):</p>
  
  <pre><code>class BookController {
  
  @Secured("ROLE_USER")   
  def create() {
    def someImage = params.imageId
    ...
  }
  @Secured("ROLE_USER")   
  def show() {
    ...
  }
  ...
  }
  </code></pre>
  
  <p>My LoginController is:</p>
  
  <pre><code>class LoginController {
  ...
  def auth() {
    ...
    redirect uri: SpringSecurityUtils.securityConfig.successHandler.defaultTargetUrl
  }
  ...
  }
  </code></pre>
  
  <p>This will redirect the user to the base URL after login was successful. I want to have the user always be redirected to the refer URL, which is the URL before he was successfully logged in.</p>
  
  <p>For example: 
  The user is not logged in. He is at home/index and want to access book/create, which is only available for logged-in users. In this case, Spring Security will redirect the user to login/auth and (after he is successfully logged in) to / (which is the base URL).</p>
  
  <p>I want to have the redirect go to the URL the user has been trying to access, which in this case is book/create. This should also be the case for any other action. If a not-logged-in user on home/index tries to access book/show/32333, he will be redirected to login/auth, and, after he successfully logs in, back to book/show/32333.</p>
  
  <p>How can I implement that?</p>