doForceHttps = true;
    public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)        throws IOException, ServletException {        HttpServletRequest req = (HttpServletRequest) request;        String scheme = request.getScheme();        String serverName = request.getServerName();        int serverPort = portResolver.getServerPort(request);        String contextPath = req.getContextPath();        boolean inHttp = "http".equals(scheme.toLowerCase());        boolean inHttps = "https".equals(scheme.toLowerCase());        boolean includePort = true;        String redirectUrl = null;        boolean doForceHttps = false;        Integer httpsPort = null;        if (inHttp && (serverPort == 80)) {            includePort = false;        } else if (inHttps && (serverPort == 443)) {            includePort = false;        }        if (forceHttps && inHttp) {            httpsPort = (Integer) portMapper.lookupHttpsPort(new Integer(serverPort));            doForceHttps = true;                    if (httpsPort != null) {                if (httpsPort.intValue() == 443) {                    includePort = false;                } else {                    includePort = true;                }            }                    }          if ( serverSideRedirect ) {            if ( doForceHttps ) {                          // before doing server side redirect, we need to do client redirect to https.                              String servletPath = req.getServletPath();                String pathInfo = req.getPathInfo();                String query = req.getQueryString();                redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath                  + servletPath + (pathInfo == null ? "" : pathInfo ) + (query == null ? "" : "?"+query );            } else {                if (logger.isDebugEnabled()) {                  logger.debug("Server side forward to: " + loginFormUrl);                }                RequestDispatcher dispatcher = req.getRequestDispatcher( loginFormUrl );                dispatcher.forward( request, response );                                return;            }        } else {            if ( doForceHttps ) {                redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath                    + loginFormUrl;            } else {                redirectUrl = scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath                  + loginFormUrl;            }        }        if (logger.isDebugEnabled()) {            logger.debug("Redirecting to: " + redirectUrl);        }        ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl));    }