Example usage for javax.servlet.http HttpServletRequest getRemoteAddr

List of usage examples for javax.servlet.http HttpServletRequest getRemoteAddr

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getRemoteAddr.

Prototype

public String getRemoteAddr();

Source Link

Document

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

Usage

From source file:org.frontcache.core.FCUtils.java

public static String getClientIP(HttpServletRequest request) {
    for (String header : CLIENT_IP_SOURCE_HEADER_LIST) {
        String ip = request.getHeader(header);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }// w  w w.j  a va  2  s  .  c  o  m
    }
    return request.getRemoteAddr();
}

From source file:io.renren.common.utils.IPUtils.java

/**
 * ?IP?// w w w .j  ava 2 s  .  com
 * 
 * Nginx???? ?request.getRemoteAddr()?IP?
 * ?????X-Forwarded-For?IP?X-Forwarded-For?unknownIPIP?
 */
public static String getIpAddr(HttpServletRequest request) {
    String ip = null;
    try {
        ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
    } catch (Exception e) {
        logger.error("IPUtils ERROR ", e);
    }

    //        //??IP?
    //        if(StringUtils.isEmpty(ip) && ip.length() > 15) {
    //         if(ip.indexOf(",") > 0) {
    //            ip = ip.substring(0, ip.indexOf(","));
    //         }
    //      }

    return ip;
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Write request variables into the dump stringbuffer
 * @param sb The dump stringbuffer//from  ww  w  .ja  va  2 s. co  m
 * @param request The HTTP request
 */
private static void dumpVariables(StringBuffer sb, HttpServletRequest request) {
    dumpVariable(sb, "AUTH_TYPE", request.getAuthType());
    dumpVariable(sb, "REQUEST_METHOD", request.getMethod());
    dumpVariable(sb, "PATH_INFO", request.getPathInfo());
    dumpVariable(sb, "PATH_TRANSLATED", request.getPathTranslated());
    dumpVariable(sb, "QUERY_STRING", request.getQueryString());
    dumpVariable(sb, "REQUEST_URI", request.getRequestURI());
    dumpVariable(sb, "SCRIPT_NAME", request.getServletPath());
    dumpVariable(sb, "LOCAL_ADDR", request.getLocalAddr());
    dumpVariable(sb, "SERVER_PROTOCOL", request.getProtocol());
    dumpVariable(sb, "REMOTE_ADDR", request.getRemoteAddr());
    dumpVariable(sb, "REMOTE_HOST", request.getRemoteHost());
    dumpVariable(sb, "HTTPS", request.getScheme());
    dumpVariable(sb, "SERVER_NAME", request.getServerName());
    dumpVariable(sb, "SERVER_PORT", String.valueOf(request.getServerPort()));
}

From source file:org.artifactory.util.HttpUtils.java

@SuppressWarnings({ "IfMayBeConditional" })
public static String getRemoteClientAddress(HttpServletRequest request) {
    String remoteAddress;//w  w w .  j a v  a2s.  co m
    //Check if there is a remote address coming from a proxied request
    //(http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypreservehost)
    String header = request.getHeader("X-Forwarded-For");
    if (StringUtils.isNotBlank(header)) {
        //Might contain multiple entries - take the first
        remoteAddress = new StringTokenizer(header, ",").nextToken();
    } else {
        //Take it the standard way
        remoteAddress = request.getRemoteAddr();
    }
    return remoteAddress;
}

From source file:fr.paris.lutece.plugins.mylutece.util.SecurityUtils.java

/**
 * Build an url to reset connection logs for an IP and a given user. Data is read from the request.
 * @param nInterval Interval of time to reset
 * @param request The request/*ww w.j  ava2 s.com*/
 * @return The url to reset connection logs.
 */
public static String buildResetConnectionLogUrl(int nInterval, HttpServletRequest request) {
    UrlItem url = new UrlItem(AppPathService.getBaseUrl(request) + JSP_URL_RESET_CONNECTION_LOG);
    String strIp = request.getRemoteAddr();
    String strDate = Long.toString(new Date().getTime());
    String strInterval = Integer.toString(nInterval);
    url.addParameter(PARAMETER_IP, strIp);
    url.addParameter(PARAMETER_DATE_LOGIN, strDate);
    url.addParameter(PARAMETER_INTERVAL, strInterval);

    String strCryptoKey = CryptoService.getCryptoKey();
    url.addParameter(PARAMETER_KEY,
            CryptoService.encrypt(strIp + strDate + strInterval + strCryptoKey,
                    AppPropertiesService.getProperty(PROPERTY_DEFAULT_ENCRYPTION_ALGORITHM,
                            CONSTANT_DEFAULT_ENCRYPTION_ALGORITHM)));

    return url.getUrl();
}

From source file:com.octo.captcha.module.web.sound.SoundToWavHelper.java

/**
 * retrieve a new SoundCaptcha using SoundCaptchaService and flush it to the response. <br/> Captcha are localized
 * using request locale. <br/>This method returns a 404 to the client instead of the image if the request isn't
 * correct (missing parameters, etc...).. <br/>The log may be null. <br/>
 *
 * @param theRequest  the request//w  ww .  j  a  v  a2s  .  c om
 * @param theResponse the response
 * @param log         a commons logger
 * @param service     an SoundCaptchaService instance
 *
 * @throws java.io.IOException if a problem occurs during the jpeg generation process
 */
public static void flushNewCaptchaToResponse(HttpServletRequest theRequest, HttpServletResponse theResponse,
        Log log, SoundCaptchaService service, String id, Locale locale) throws IOException {

    // call the ImageCaptchaService method to retrieve a captcha
    byte[] captchaChallengeAsWav = null;
    ByteArrayOutputStream wavOutputStream = new ByteArrayOutputStream();
    try {
        AudioInputStream stream = service.getSoundChallengeForID(id, locale);

        // call the ImageCaptchaService method to retrieve a captcha

        AudioSystem.write(stream, AudioFileFormat.Type.WAVE, wavOutputStream);
        //AudioSystem.(pAudioInputStream, AudioFileFormat.Type.WAVE, pFile);

    } catch (IllegalArgumentException e) {
        //    log a security warning and return a 404...
        if (log != null && log.isWarnEnabled()) {
            log.warn("There was a try from " + theRequest.getRemoteAddr()
                    + " to render an captcha with invalid ID :'" + id + "' or with a too long one");
            theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    } catch (CaptchaServiceException e) {
        // log and return a 404 instead of an image...
        if (log != null && log.isWarnEnabled()) {
            log.warn(

                    "Error trying to generate a captcha and " + "render its challenge as JPEG", e);
        }
        theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    captchaChallengeAsWav = wavOutputStream.toByteArray();

    // render the captcha challenge as a JPEG image in the response
    theResponse.setHeader("Cache-Control", "no-store");
    theResponse.setHeader("Pragma", "no-cache");
    theResponse.setDateHeader("Expires", 0);

    theResponse.setContentType("audio/x-wav");
    ServletOutputStream responseOutputStream = theResponse.getOutputStream();
    responseOutputStream.write(captchaChallengeAsWav);
    responseOutputStream.flush();
    responseOutputStream.close();
}

From source file:com.gson.web.WeChatPayServlet.java

/**
 * ?ip/*from  w  w w.j ava  2  s .c  om*/
 * @param request
 * @return
 */
public static String getIp(HttpServletRequest request) {
    if (request == null)
        return "";
    String ip = request.getHeader("X-Requested-For");
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Forwarded-For");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("HTTP_CLIENT_IP");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

From source file:com.shishu.utility.string.StringUtil.java

/** 
 * ?IP,??IPBUG/*from   w ww  .  j av  a 2s .  co  m*/
 * @param request
 * @return 
 * @create  2009-2-24 ?09:19:02 yanghb
 * @history  
 */
public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

From source file:com.whatlookingfor.common.utils.StringUtils.java

/**
 * ?IP//ww  w  .ja  va2s.c  om
 *
 * @param request 
 * @return ip?
 */
public static final String getHost(HttpServletRequest request) {
    String ip = request.getHeader("X-Forwarded-For");
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Real-IP");
    }
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    if ("127.0.0.1".equals(ip)) {
        InetAddress inet = null;
        try { // ????IP
            inet = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        ip = inet.getHostAddress();
    }
    // ?IPIP,IP','
    if (ip != null && ip.length() > 15) {
        if (ip.indexOf(",") > 0) {
            ip = ip.substring(0, ip.indexOf(","));
        }
    }
    return ip;
}

From source file:uk.ac.soton.itinnovation.sad.service.controllers.GenericController.java

@ModelAttribute("clientIpAddress")
private String populateClientIpAddress(HttpServletRequest request) {

    return request.getRemoteAddr();
}