title: too many redirects using filter class
body: <p>JSF 2.2 and Primefaces 6.0</p>
  
  <p>I'm trying to use filter class for authentication session control. But filter class running 21 times and browser set error message ERR_TOO_MANY_REDIRECTS. </p>
  
  <p>Web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"&gt;
      &lt;display-name&gt;maintenancemonitoring&lt;/display-name&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;/param-name&gt;
          &lt;param-value&gt;.xhtml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;description&gt;State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2&lt;/description&gt;
          &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
          &lt;param-value&gt;client&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.servlet.jsp.jstl.fmt.localizationContext&lt;/param-name&gt;
          &lt;param-value&gt;resources.application&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;authFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;view.filters.AuthenticationFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;authFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
          &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
      &lt;/filter-mapping&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
          &lt;param-value&gt;Development&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;primefaces.THEME&lt;/param-name&gt;
          &lt;param-value&gt;cupertino&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.xhtml&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Filter class : </p>
  
  <pre><code>public void
          doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException,
                                                                                                           ServletException {
      HttpServletRequest request = (HttpServletRequest) servletRequest;
      request.setCharacterEncoding("UTF-8");
      HttpServletResponse response = (HttpServletResponse) servletResponse;
      HttpSession session = request.getSession();
  
      System.out.println("aaaaa");
  
      String currentLoginId = null;
      if(session.getAttribute("currentLoginId")!=null){
          currentLoginId = (String) session.getAttribute("currentLoginId");
      }
  
      if(currentLoginId != null){
          setResponseHeaders(response);
          filterChain.doFilter(request, response);
      } else {
          response.sendRedirect(request.getContextPath() + "/faces/login.xhtml");
      }
  }
  
  private void setResponseHeaders(HttpServletResponse httpResponse) {
      httpResponse.addHeader("Pragma", "no-cache");
      httpResponse.addHeader("Cache-Control", "no-cache");
      httpResponse.addHeader("Cache-Control", "must-revalidate");
      httpResponse.addHeader("Cache-Control", "post-check=0");
      httpResponse.addHeader("Cache-Control", "pre-check=0");
      httpResponse.addHeader("Cache-Control", "no-store");
      httpResponse.addDateHeader("Expires", 0);
  }
  </code></pre>
  
  <p>Login action in loginBean:</p>
  
  <pre><code>public String actionLogin(ActionEvent actionEvent) throws ServletException, IOException {
      HttpServletRequest request = (HttpServletRequest) getExternalContext().getRequest();
      HttpServletResponse response = (HttpServletResponse) getExternalContext().getResponse();
  
      AuthUser user = getValidUser();
  
      request.setAttribute("user", user);
  
      if (user == null) {
          addMessage("Kullanıcı adı ya da şifre hatalı");
          return null;
      }
  
      return handleUserLogin(user, request, response);
  }
  </code></pre>
  
  <p>Create session in loginHandler class:</p>
  
  <pre><code>private String createNewSessionAndRedirect(HttpServletRequest request, HttpServletResponse response, AuthUser user) {
      HttpSession session = getSessionForRequest(request);
  
      session.setAttribute("currentLoginId", user.getUserName());
  
      if (request.isRequestedSessionIdValid() &amp;&amp; session != null &amp;&amp; session.getAttribute("currentLoginId") != null) {
          try {
              response.sendRedirect(request.getContextPath() + "/faces/welcome.xhtml");
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      return "/welcome.xhtml";
  }
  </code></pre>