Example usage for org.springframework.security.web.util RedirectUrlBuilder RedirectUrlBuilder

List of usage examples for org.springframework.security.web.util RedirectUrlBuilder RedirectUrlBuilder

Introduction

In this page you can find the example usage for org.springframework.security.web.util RedirectUrlBuilder RedirectUrlBuilder.

Prototype

RedirectUrlBuilder

Source Link

Usage

From source file:br.com.sg.security.SgLoginUrlAuthenticationEntryPoint.java

/**
 * Builds a URL to redirect the supplied request to HTTPS. Used to redirect
 * the current request to HTTPS, before doing a forward to the login page.
 *///  ww w  .  j  av a  2s  . com
protected String buildHttpsRedirectUrlForRequest(HttpServletRequest request)
        throws IOException, ServletException {

    Integer serverPort = portResolver.getServerPort(request);
    Integer httpsPort = portMapper.lookupHttpsPort(new Integer(serverPort));

    if (httpsPort != null) {
        RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();
        urlBuilder.setScheme("https");
        urlBuilder.setServerName(request.getServerName());
        urlBuilder.setPort(httpsPort.intValue());
        urlBuilder.setContextPath(request.getContextPath());
        urlBuilder.setServletPath(request.getServletPath());
        urlBuilder.setPathInfo(request.getPathInfo());
        urlBuilder.setQuery(request.getQueryString());

        return urlBuilder.getUrl();
    }

    // Fall through to server-side forward with warning message
    logger.warn("Unable to redirect to HTTPS as no port mapping found for HTTP port " + serverPort);

    return null;
}

From source file:org.fluxtream.core.auth.FlxLoginUrlAuthenticationEntryPoint.java

protected String buildRedirectUrlToLoginPage(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException {

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

    if (UrlUtils.isAbsoluteUrl(loginForm)) {
        return loginForm;
    }//from   www  .j a  v a  2s .c o  m

    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();
}

From source file:org.opendatakit.common.security.spring.RedirectingLoginUrlAuthenticationEntryPoint.java

protected String buildRedirectUrlToLoginPage(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) {

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

    if (UrlUtils.isAbsoluteUrl(loginForm)) {
        return loginForm;
    }//  w  w  w  . j a  v  a  2 s  .  c o m

    int serverPort = getPortResolver().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);
    try {
        String fullRequest = request.getRequestURL().toString();
        if (request.getQueryString() != null) {
            fullRequest += "?" + request.getQueryString();
        }
        String redirectParam = "redirect=" + URLEncoder.encode(fullRequest, "UTF-8");
        urlBuilder.setQuery(redirectParam);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    if (isForceHttps() && "http".equals(scheme)) {
        Integer httpsPort = getPortMapper().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();
}