title: ERR_TOO_MANY_REDIRECTS from Shiro security AuthController in Grails app
body: <p>Shiro security works great with my grails app with the exception of if a user enters incorrect login information. If they put in the wrong username and password combination (which is authenticated via an LDAP server), it seems that I'm entering an infinite loop between the index and login actions. The general form of my AuthController is</p>
  
  <pre><code>class AuthController 
  {
      def shiroSecurityManager
  
      def index =
      {
          println "in index..."
          redirect(action: "login", params: params) 
      }
  
      def login =
      {     
          println "in login..."
          return [ username: params.username, rememberMe: (params.rememberMe != null), targetUri: params.targetUri ]
      }
  
      def signIn =
      {
          println "in signin..."
          Subject subject = SecurityUtils.getSubject()
          request.getSession().removeAttribute("subject")
          request.getSession(true).setAttribute("subject", subject)
          String lowerCaseUserName=params.username.toLowerCase()
          def authToken = new UsernamePasswordToken(lowerCaseUserName, params.password)
  
          // Support for "remember me"
          if (params.rememberMe) 
          {
              authToken.rememberMe = true
          }
  
          try
          {
              println "in signin try..."
              subject.login(authToken)
              if (subject.isAuthenticated()) 
              {
                  println "in signin try if 1..."
                  def currentUser = User.findByEmployeeLogin(lowerCaseUserName)
  
                  if (currentUser == null)
                  {
                      log.info "Authentication failure for user '${lowerCaseUserName}'."
                      flash.message = message(code: "login.failed")
  
                      // Remember the target URI too.
                      if (params.targetUri) 
                      {
                          m['targetUri'] = params.targetUri
                          redirect(controller: 'auth', action: 'login', params: m)
                      }
                      else
                      {
                          redirect(controller: 'auth', action: 'login')
                      }
                  }
                  else
                  {
                      ...
                      redirect(controller: 'home', action: 'index')
                  }
              }
          }
          catch (AuthenticationException ex)
          {
              println "in signin catch..."
              // Authentication failed, so display the appropriate message
              // on the login page.
              log.info "Authentication failure for user '${lowerCaseUserName}'."
              flash.message = message(code: "login.failed")
  
              // Keep the username and "remember me" setting so that the
              // user doesn't have to enter them again.
              def m = [ username: lowerCaseUserName ]
              if (params.rememberMe) 
              {
                  m['rememberMe'] = true
              }
  
              // Remember the target URI too.
              if (params.targetUri) 
              {
                  m['targetUri'] = params.targetUri
              }
  
              // Now redirect back to the login page.
              redirect(controller: 'auth', action: 'login', params: m)
          }
      }
  }
  </code></pre>
  
  <p>The println's produce the following output</p>
  
  <pre><code>in signin...
  in signin try...
  Could not connect to &lt;MYLDAPSERVER&gt;: javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1 ]
  Could not connect to &lt;MYLDAPSERVER&gt;: javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1 ]
  No LDAP server available.
  in signin catch...
  in login...
  in index...
  in login...
  in index...
  in login...
  in index...
  in login...
  </code></pre>
  
  <p>The login/index output just keeps going until I get the error</p>
  
  <pre><code>This webpage has a redirect loop
  
  ERR_TOO_MANY_REDIRECTS
  </code></pre>
  
  <p>Does anyone see why I'm having this issue?</p>
  
  <p>Edit: I believe it may be due to my SecurityFilters baseFilter:</p>
  
  <pre><code>    baseFilter(controller: "*", action: "*") {
          before = {                
              /*====================================================================
               * if auth controller then ok to continue
               *===================================================================*/
              if (controllerName.equals("auth"))
              {
                  println "1.."
                  return true;
              }
  
              /*=====================================================================
               * If no subject (user) and not auth controller
               * then user must authenticate
               *===================================================================*/
              if (!session.subject &amp;&amp; !(controllerName.equals("auth")))
              {
                  println "2.."
                  params.targetUri = request.forwardURI
                  redirect(controller:'auth', action:'login', params: params)
                  return false;
              }
          }
  
          after = {
              try
              { 
                  println "3.."
                  if (!(session.subject.isAuthenticated()))
                  {
                      println "4.."
                      redirect(controller:'auth', action:'login', params: params)
                      return false;
                  }
              }
              catch(Exception e)
              {
                  println "5.."
              }
          }
      }
  </code></pre>
  
  <p>This results in the following output</p>
  
  <pre><code>in login...
  3..
  4..
  1..
  in login...
  3..
  4..
  1..
  in login...
  3..
  4..
  1..
  </code></pre>
  
  <p>So it looks like I've found my issue, but I'm not sure how to fix it still.</p>