@Override
  public boolean isFilterEnabled(HttpServletRequest request, HttpServletResponse response) {

    try {
      long companyId = PortalUtil.getCompanyId(request);

      OpenSSOConfiguration openSSOConfiguration = getOpenSSOConfiguration(companyId);

      if (openSSOConfiguration.enabled()
          && Validator.isNotNull(openSSOConfiguration.loginURL())
          && Validator.isNotNull(openSSOConfiguration.logoutURL())
          && Validator.isNotNull(openSSOConfiguration.serviceURL())) {

        return true;
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    return false;
  }
  @Override
  protected void processFilter(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws Exception {

    long companyId = PortalUtil.getCompanyId(request);

    OpenSSOConfiguration openSSOConfiguration = getOpenSSOConfiguration(companyId);

    String requestURI = GetterUtil.getString(request.getRequestURI());

    if (requestURI.endsWith("/portal/logout")) {
      HttpSession session = request.getSession();

      session.invalidate();

      response.sendRedirect(openSSOConfiguration.logoutURL());

      return;
    }

    boolean authenticated = false;

    try {

      // LEP-5943

      authenticated = _openSSO.isAuthenticated(request, openSSOConfiguration.serviceURL());
    } catch (Exception e) {
      _log.error(e, e);

      processFilter(OpenSSOFilter.class, request, response, filterChain);

      return;
    }

    HttpSession session = request.getSession();

    if (authenticated) {

      // LEP-5943

      String newSubjectId = _openSSO.getSubjectId(request, openSSOConfiguration.serviceURL());

      String oldSubjectId = (String) session.getAttribute(_SUBJECT_ID_KEY);

      if (oldSubjectId == null) {
        session.setAttribute(_SUBJECT_ID_KEY, newSubjectId);
      } else if (!newSubjectId.equals(oldSubjectId)) {
        session.invalidate();

        session = request.getSession();

        session.setAttribute(_SUBJECT_ID_KEY, newSubjectId);
      }

      processFilter(OpenSSOFilter.class, request, response, filterChain);

      return;
    } else if (PortalUtil.getUserId(request) > 0) {
      session.invalidate();
    }

    if (!PropsValues.AUTH_FORWARD_BY_LAST_PATH
        || !openSSOConfiguration.loginURL().contains("/portal/login")) {

      response.sendRedirect(openSSOConfiguration.loginURL());

      return;
    }

    String currentURL = PortalUtil.getCurrentURL(request);

    String redirect = currentURL;

    if (currentURL.contains("/portal/login")) {
      redirect = ParamUtil.getString(request, "redirect");

      if (Validator.isNull(redirect)) {
        redirect = PortalUtil.getPathMain();
      }
    }

    redirect =
        openSSOConfiguration.loginURL()
            + HttpUtil.encodeURL("?redirect=" + HttpUtil.encodeURL(redirect));

    response.sendRedirect(redirect);
  }