@Override
  protected void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    boolean toLoginPage = true;
    Cookie loginCookie = HttpUtils.getLoginCookie(req);
    if (loginCookie != null) {
      LOG.debug("Request has a '{}' cookie", HttpUtils.getLoginCookieName());

      String tokenStr = loginCookie.getValue();
      if (tokenStr != null) {
        SSOPrincipal principal = getSsoService().validateUserToken(tokenStr);
        if (principal != null) {
          LOG.debug(
              "Request already has an authenticated user '{}', skipping login page",
              principal.getName());
          res.setHeader(SSOConstants.X_USER_AUTH_TOKEN, tokenStr);
          String redirectUrl = req.getParameter(SSOConstants.REQUESTED_URL_PARAM);
          if (redirectUrl != null) {
            boolean repeatedRedirect =
                req.getParameter(SSOConstants.REPEATED_REDIRECT_PARAM) != null;
            if (repeatedRedirect) {
              LOG.warn("Request is a repeated redirect, invalidating token '{}'", tokenStr);
              getSsoService().invalidateUserToken(tokenStr);
            } else {
              LOG.debug("Redirecting back to '{}'", redirectUrl);
              redirectUrl = createRedirectionUrl(redirectUrl, tokenStr);
              res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
              res.setHeader("Pragma", "no-cache");
              res.setHeader("Expires", "0");
              res.sendRedirect(redirectUrl);
              toLoginPage = false;
            }
          } else {
            res.setStatus(HttpServletResponse.SC_ACCEPTED);
            toLoginPage = false;
          }
        } else {
          LOG.debug(
              "Request has an invalid '{}' cookie '{}'", HttpUtils.getLoginCookieName(), tokenStr);
        }
      }
    }

    if (toLoginPage) {
      dispatchToLoginPage(req, res);
    }
  }
  @Override
  public Principal resolve(Credential credential) {

    UsernamePasswordCredential usernamePasswordCredential = (UsernamePasswordCredential) credential;

    String username = usernamePasswordCredential.getUsername();

    String[] args = {username, username, username};

    List result = jdbcTemplate.queryForList(SQL, args);

    SSOPrincipal principal = new SSOPrincipal();

    for (Object obj : result) {
      Map map = (Map) obj;
      principal.setId((String) map.get("userId"));
      principal.setAttributes(map);
    }

    return principal;
  }