public void afterPropertiesSet() throws Exception {
   Assert.isTrue(
       StringUtils.hasText(loginFormUrl) && UrlUtils.isValidRedirectUrl(loginFormUrl),
       "loginFormUrl must be specified and must be a valid redirect URL");
   if (useForward && UrlUtils.isAbsoluteUrl(loginFormUrl)) {
     throw new IllegalArgumentException(
         "useForward must be false if using an absolute loginFormURL");
   }
   Assert.notNull(portMapper, "portMapper must be specified");
   Assert.notNull(portResolver, "portResolver must be specified");
 }
 protected String calculateRelativeRedirectUrl(final String contextPath, final String url) {
   if (UrlUtils.isAbsoluteUrl(url)) {
     String relUrl = url.substring(url.indexOf("://") + 3);
     String modifiedContextPath = contextPath;
     final String urlEncodingAttributes =
         getSessionService().getAttribute(WebConstants.URL_ENCODING_ATTRIBUTES);
     if (urlEncodingAttributes != null
         && !url.contains(urlEncodingAttributes)
         && modifiedContextPath.contains(urlEncodingAttributes)) {
       modifiedContextPath = StringUtils.remove(modifiedContextPath, urlEncodingAttributes);
     }
     if (StringUtils.isEmpty(relUrl) || StringUtils.isEmpty(modifiedContextPath)) {
       relUrl = "/";
     } else {
       relUrl =
           relUrl.substring(relUrl.indexOf(modifiedContextPath) + modifiedContextPath.length());
     }
     return relUrl;
   } else {
     return url;
   }
 }
  protected String buildRedirectUrlToLoginPage(
      HttpServletRequest request,
      HttpServletResponse response,
      AuthenticationException authException) {

    String loginForm = determineUrlToUseForThisRequest(request, response, authException);

    if (UrlUtils.isAbsoluteUrl(loginForm)) {
      return loginForm;
    }

    int serverPort = portResolver.getServerPort(request);
    String scheme = request.getScheme();

    RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();

    urlBuilder.setScheme(scheme);
    urlBuilder.setServerName(request.getServerName());
    urlBuilder.setPort(serverPort);
    urlBuilder.setContextPath(request.getContextPath());
    urlBuilder.setPathInfo(loginForm);

    if (forceHttps && "http".equals(scheme)) {
      Integer httpsPort = portMapper.lookupHttpsPort(Integer.valueOf(serverPort));

      if (httpsPort != null) {
        // Overwrite scheme and port in the redirect URL
        urlBuilder.setScheme("https");
        urlBuilder.setPort(httpsPort.intValue());
      } else {
        logger.warn(
            "Unable to redirect to HTTPS as no port mapping found for HTTP port " + serverPort);
      }
    }

    return urlBuilder.getUrl();
  }