我如何可以检索从Java HTTP标头的IP地址[复制]我如何可以检索从Java HTTP标头的IP

2019-05-13 17:09发布

这个问题已经在这里有一个答案:

  • 我如何在servlet的客户端的远程地址? 10个回答

我很好奇,如果有已经处理这种东西任何库,或我有自己再次做到这一点。 那么,事情是我想从我的服务器上的游客HTTP头请求获取IP地址字段,做整个事情在Java中? 你能帮忙的话,我会很高兴。 提前致谢。

Answer 1:

使用getHeader(String Name)的方法javax.servlet.http.HttpServletRequest对象检索的值Remote_Addr变量。 下面是示例代码:

String ipAddress = request.getHeader("Remote_Addr");

如果这个代码返回空字符串,然后用这样的方式:

String ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");

if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}


Answer 2:

即使有已高度upvoted我想建议的替代,并指出接受的答案的缺点,一个公认的答案。

request.getHeader("Remote_Addr")被指定到返回完全一样request.getRemoteAddr() 因此,它是没有意义的同时检查。 还要注意的是getRemoteAddr是的方法javax.servlet.ServletRequest (即HTTP无关),而getHeaderjavax.servlet.http.HttpServletRequest

此外,有些代理服务器使用Client-IP ,而不是X-Forwarded-For 。 对于讨论,见https://stackoverflow.com/a/7446010/131929 。

我不知道该用怎样可靠HTTP_X_FORWARDED_FORX-Forwarded-For是。 在Java中,我宁愿用直接,短表。 对于讨论,见https://stackoverflow.com/a/3834169/131929 。 大写/小写没有区别,因为getHeader被指定为情况敏感。

Java的替代

public final class ClientIpAddress {

  // CHECKSTYLE:OFF
  // https://stackoverflow.com/a/11327345/131929
  private static Pattern PRIVATE_ADDRESS_PATTERN = Pattern.compile(
      "(^127\\.)|(^192\\.168\\.)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^::1$)|(^[fF][cCdD])",
      Pattern.CANON_EQ);
  // CHECKSTYLE:ON

  private ClientIpAddress() {
  }

  /**
   * Extracts the "real" client IP address from the request. It analyzes request headers
   * {@code REMOTE_ADDR}, {@code X-Forwarded-For} as well as {@code Client-IP}. Optionally
   * private/local addresses can be filtered in which case an empty string is returned.
   *
   * @param request HTTP request
   * @param filterPrivateAddresses true if private/local addresses (see
   * https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces and
   * https://en.wikipedia.org/wiki/Unique_local_address) should be filtered i.e. omitted
   * @return IP address or empty string
   */
  public static String getFrom(HttpServletRequest request, boolean filterPrivateAddresses) {
    String ip = request.getRemoteAddr();

    String headerClientIp = request.getHeader("Client-IP");
    String headerXForwardedFor = request.getHeader("X-Forwarded-For");
    if (StringUtils.isEmpty(ip) && StringUtils.isNotEmpty(headerClientIp)) {
      ip = headerClientIp;
    } else if (StringUtils.isNotEmpty(headerXForwardedFor)) {
      ip = headerXForwardedFor;
    }
    if (filterPrivateAddresses && isPrivateOrLocalAddress(ip)) {
      return StringUtils.EMPTY;
    } else {
      return ip;
    }
  }

  private static boolean isPrivateOrLocalAddress(String address) {
    Matcher regexMatcher = PRIVATE_ADDRESS_PATTERN.matcher(address);
    return regexMatcher.matches();
  }
}

PHP替代

function getIp()
{
    $ip = $_SERVER['REMOTE_ADDR'];

    if (empty($ip) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // omit private IP addresses which a proxy forwarded
        $tmpIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
        $tmpIp = filter_var(
            $tmpIp,
            FILTER_VALIDATE_IP,
            FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
        );
        if ($tmpIp != false) {
            $ip = $tmpIp;
        }
    }
    return $ip;
}


文章来源: How can I retrieve IP address from HTTP header in Java [duplicate]