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.comcast.cdn.traffic_control.traffic_router.api.util.APIAccessRecord.java

public static void log(final HttpServletRequest request) {
    ACCESS.info(String.format(ACCESS_FORMAT, FORMATTER.format(new Date()), request.getRemoteAddr(),
            request.getMethod(), getUrl(request)));
}

From source file:com.trenako.web.controllers.ErrorController.java

private final static boolean isLocalhost(HttpServletRequest request) {
    if (request == null)
        return false;

    return request.getRemoteAddr().equals("localhost") || request.getRemoteAddr().equals("127.0.0.1");
}

From source file:com.netflix.spinnaker.gate.interceptors.RequestLoggingInterceptor.java

private static String sourceIpAddress(HttpServletRequest request) {
    String ip = request.getHeader("X-FORWARDED-FOR");
    if (ip == null) {
        return request.getRemoteAddr();
    }//from   w w  w  . jav  a  2 s  . co  m
    return ip;
}

From source file:edu.usu.sdl.openstorefront.common.util.NetworkUtil.java

/**
 * Get the correct client ip from a request
 *
 * @param request/*from  w  ww  . j  ava 2s.c o  m*/
 * @return client ip or N/A if not found
 */
public static String getClientIp(HttpServletRequest request) {
    String clientIp = OpenStorefrontConstant.NOT_AVAILABLE;
    if (request != null) {
        clientIp = request.getRemoteAddr();

        //Check for header ip it may be forwarded by a proxy
        String clientIpFromHeader = request.getHeader("x-forwarded-for");
        if (StringUtils.isNotBlank(clientIpFromHeader)) {
            clientIp = clientIp + " Forward for: " + clientIpFromHeader;
        } else {
            clientIpFromHeader = request.getHeader("x-real-ip");
            if (StringUtils.isNotBlank(clientIpFromHeader)) {
                clientIp = clientIp + " X-real IP: " + clientIpFromHeader;
            }
        }
    }
    return clientIp;
}

From source file:com.inkubator.securitycore.util.UserInfoUtil.java

public static String getRequestRemoteAddr() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();/*from w ww .j  av  a  2  s.  c  o m*/
    return request.getRemoteAddr();
}

From source file:com.neocotic.blingaer.common.RequestUtils.java

/**
 * Returns the IP address that appears to be the source of the specified
 * {@code request}.//ww  w .j a  v  a 2s  . c  o m
 * 
 * @param request
 *            the {@code HttpServletRequest} to check
 * @return the IP address
 */
public static String getIpAddress(HttpServletRequest request) {
    String ipAddress = request.getHeader("X-Forwarded-For");

    if (ipAddress == null) {
        ipAddress = request.getRemoteAddr();
    } else {
        String[] ipAddresses = StringUtils.split(ipAddress, ",");

        if (ipAddresses.length > 0) {
            ipAddress = ipAddresses[0];
        }
    }

    return ipAddress;
}

From source file:com.binarybirchtree.filters.IpFilter.java

public static String getForwardedIp(HttpServletRequest request, int degree) {
    if (degree == 0) {
        return request.getRemoteAddr();
    }//w  w  w  .  j  av a 2 s .  com

    // Believe the X-Forwarded-For header only if it came from a trusted internal IP.
    if (!ipIsInList(request.getRemoteAddr(), internalIps)) {
        System.err.println(
                "Last IP address was " + request.getRemoteAddr() + ", which is not a trusted internal IP.");
        return null;
    }

    // Return null if the X-Forwarded-For header was not found.
    String header = request.getHeader("X-Forwarded-For");
    if (header == null)
        return null;

    String[] ips = header.split(",");
    if (degree > ips.length)
        return null;
    return ips[ips.length - degree].trim();
}

From source file:net.duckling.ddl.util.ClientValidator.java

public static String getRealIp(HttpServletRequest request) {
    String nginxIp = request.getHeader("x-real-ip");
    if (StringUtils.isNotEmpty(nginxIp)) {
        return nginxIp;
    }/*  w  w w.  j  a  v a2s.c o m*/
    return request.getRemoteAddr();
}

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

public static String getRequestRemoteAddrBySpring() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();/*from  w  ww  .  j  ava2 s  . c om*/
    return request.getRemoteAddr();
}

From source file:am.ik.categolj2.core.web.RemoteAddresses.java

public static String getRemoteAddress(HttpServletRequest request) {
    String forwardedFor = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
    String result;//from  w  w w . ja  v a2  s.  co m
    if (forwardedFor != null) {
        result = forwardedFor;
    } else {
        result = request.getRemoteAddr();
    }
    return StringUtils.substring(result, 0, 128);
}