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:com.ericsson.eif.hansoft.HansoftManager.java

/**
 * @param httpServletRequest/*from w ww.j ava2 s. c o  m*/
 * @return prefix
 */
public static String getPrefix(HttpServletRequest httpServletRequest) {
    if (httpServletRequest == null) {
        return "";
    }
    // check if IP is in list of known FocalPoints
    String ip = httpServletRequest.getRemoteAddr();
    if (ip == null) {
        ip = "";
    }

    if (ip != null && !ip.trim().equals("")) {
        if (hansoftFPs.contains(ip)) {
            return Constants.FP_FIX_PREFIX;
        }
    }

    // check if hostname is in list of known FocalPoints
    try {
        String hostname = InetAddress.getByName(ip).getHostName();
        if (hostname != null && !hostname.trim().equals("")) {
            if (hansoftFPs.contains(hostname)) {
                return Constants.FP_FIX_PREFIX;
            }
        }
    } catch (UnknownHostException e) {
        logger.error("Unknown host name: " + e.getMessage());
    }
    return "";
}

From source file:ru.org.linux.csrf.CSRFProtectionService.java

/**
 * Check if user is authorized for request
 * @param request//from w  w  w .ja  v a  2s  .  com
 * @return true when ok, false when not authorized
 */
public static boolean checkCSRF(HttpServletRequest request) {
    String cookieValue = (String) request.getAttribute(CSRF_ATTRIBUTE);

    if (Strings.isNullOrEmpty(cookieValue)) {
        logger.info("Missing CSRF cookie");
        return false;
    }

    String inputValue = request.getParameter(CSRF_INPUT_NAME);

    if (Strings.isNullOrEmpty(inputValue)) {
        logger.info("Missing CSRF input");
        return false;
    }

    boolean r = inputValue.trim().equals(cookieValue.trim());

    if (!r) {
        logger.info(String.format("Flood protection (CSRF cookie differs: cookie=%s param=%s) ip=%s url=%s",
                cookieValue, inputValue, request.getRemoteAddr(), request.getRequestURI()));
    }

    return r;
}

From source file:com.eastcom.baseframe.web.common.utils.Servlets.java

/**
 * ?/*  w ww  .j  a  v a 2s  . c om*/
 * 
 * @param request
 * @return
 */
public static String getRemoteAddr(HttpServletRequest request) {
    String remoteAddr = request.getHeader("X-Real-IP");
    if (StringUtils.isNotBlank(remoteAddr)) {
        remoteAddr = request.getHeader("X-Forwarded-For");
    } else if (StringUtils.isNotBlank(remoteAddr)) {
        remoteAddr = request.getHeader("Proxy-Client-IP");
    } else if (StringUtils.isNotBlank(remoteAddr)) {
        remoteAddr = request.getHeader("WL-Proxy-Client-IP");
    }
    return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
}

From source file:com.ai.smart.common.helper.util.RequestUtils.java

/**
 * ?IP//from  w w w  .j a va  2s  .co  m
 * <p>
 * Request.getRemoteAddr()???nginx?????
 * <p>
 * Header?X-Real-IP??X-Forwarded-ForIP(,)
 * ?Request .getRemoteAddr()
 *
 * @param request
 * @return
 */
public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("X-Real-IP");
    if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
        return ip;
    }
    ip = request.getHeader("X-Forwarded-For");
    if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
        // ?????IPIP
        int index = ip.indexOf(',');
        if (index != -1) {
            return ip.substring(0, index);
        } else {
            return ip;
        }
    } else {
        return request.getRemoteAddr();
    }
}

From source file:com.github.ipaas.ifw.util.IPUtil.java

/**
 * ???IP/*from w  ww.  j  a v  a 2  s  .c  o  m*/
 * 
 * @param request
 *            http
 * @param mode
 *            0:user ip, 1:wap user ip, 2: nearest client ip
 * @return
 */
private static String getClientIP(HttpServletRequest request, int mode) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    } else {
        String[] ips = ip.split(",");
        ip = ips[0].trim();
    }

    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    } else {
        return ip.trim();
    }

    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }

    return ip.trim();
}

From source file:com.liusoft.dlog4j.UserLoginManager.java

/**
 * //w w  w. j  ava 2s . c  o m
 * 
 * @param req
 * @param res
 * @param ubean
 * @param keepDays
 * @see com.liusoft.dlog4j.action.UserAction#doLogin(ActionMapping,
 *      ActionForm, HttpServletRequest, HttpServletResponse)
 */
public static SessionUserObject loginUser(HttpServletRequest req, HttpServletResponse res, UserBean ubean,
        int keepDays) {
    HttpSession ssn = req.getSession(false);
    if (ssn != null) {
        SessionUserObject rub = (SessionUserObject) ssn.getAttribute(SESSION_USER_KEY);
        if (rub != null && rub.getId() == ubean.getId()) {
            return rub;
        }
    }

    // ??,??
    ubean.setLastAddr(req.getRemoteAddr());
    ubean.setLastTime(new Timestamp(System.currentTimeMillis()));
    ubean.setKeepDays(keepDays);
    ubean.setOnlineStatus(1);
    DLOGUserManager.update(ubean);

    // ?cookie,?session?
    UUID uuid = new UUID();
    uuid.uid = ubean.getId();
    uuid.pwdCode = ubean.getPassword().hashCode();
    uuid.host = req.getRemoteAddr();

    String value = uuid.toString();
    RequestUtils.setCookie(req, res, COOKIE_UUID_KEY, value, (keepDays > 0) ? keepDays * 86400 : -1);
    RequestUtils.setCookie(req, res, COOKIE_LASTLOGIN_KEY, ubean.getLastTime().toString(), -1);

    // ?Session
    if (ssn == null)
        ssn = req.getSession(true);
    if (ssn != null && ubean != null) {
        ssn.setAttribute(SESSION_USER_KEY, SessionUserObject.copyFrom(ubean));
    }
    return ubean;
}

From source file:com.octo.captcha.module.web.image.ImageToJpegHelper.java

/**
 * retrieve a new ImageCaptcha using ImageCaptchaService and flush it to the response.<br/> Captcha are localized
 * using request locale.<br/>//  w w w  .j a v a2s  . co  m
 * <p/>
 * 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
 * @param theResponse the response
 * @param log         a commons logger
 * @param service     an ImageCaptchaService instance
 *
 * @throws java.io.IOException if a problem occurs during the jpeg generation process
 */
public static void flushNewCaptchaToResponse(HttpServletRequest theRequest, HttpServletResponse theResponse,
        Log log, ImageCaptchaService service, String id, Locale locale) throws IOException {

    // call the ImageCaptchaService method to retrieve a captcha
    byte[] captchaChallengeAsJpeg = null;
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
    try {
        BufferedImage challenge = service.getImageChallengeForID(id, locale);
        // the output stream to render the captcha image as jpeg into

        ImageIO.write(challenge, "jpg", jpegOutputStream);

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

    captchaChallengeAsJpeg = jpegOutputStream.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("image/jpeg");
    ServletOutputStream responseOutputStream = theResponse.getOutputStream();
    responseOutputStream.write(captchaChallengeAsJpeg);
    responseOutputStream.flush();
    responseOutputStream.close();
}

From source file:com.inkubator.hrm.util.IpUtil.java

public static String getClientIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("X-Forwarded-For");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }//from w  ww.j a  v a 2s  . co m
    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.getHeader("HTTP_CLIENT_IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    LOGGER.info(request.getHeader("X-Forwarded-For"));
    LOGGER.info(request.getHeader("Proxy-Client-IP"));
    LOGGER.info(request.getHeader("WL-Proxy-Client-IP"));
    LOGGER.info(request.getHeader("HTTP_CLIENT_IP"));
    LOGGER.info(request.getHeader("HTTP_X_FORWARDED_FOR"));
    return ip;
}

From source file:com.fengduo.spark.commons.util.HttpUtil.java

/**
 * IP?/*from  ww w .  j  a v a2  s .c o  m*/
 * 
 * @param request
 * @return
 */
public static String toIpAddr(HttpServletRequest request) {
    if (request == null) {
        return "unknown";
    }
    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("X-Forwarded-For");
    }
    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.getHeader("X-Real-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

From source file:com.fengduo.bee.commons.util.HttpUtil.java

/**
 * IP?/*from w w  w.j  a v  a  2 s  .  co m*/
 * 
 * @param request
 * @return
 */
public static String toIpAddr(HttpServletRequest request) {
    if (request == null) {
        return "unknown";
    }

    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("X-Forwarded-For");
    }
    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.getHeader("X-Real-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}