WebUtils { public static String getRealClientIpAddr(final Map<String, String> httpHeaders, final String remoteAddr) { String clientIp = httpHeaders.getOrDefault(X_FORWARDED_FOR_HEADER, null); if (clientIp == null) { clientIp = remoteAddr; } else { final String[] ips = clientIp.split(","); clientIp = ips[0]; } final String[] parts = clientIp.split(":"); clientIp = parts[0]; return clientIp; }  static String displayBytes(final long sizeBytes); static String formatStatus(final Status status); static String getRealClientIpAddr(final Map<String, String> httpHeaders,
      final String remoteAddr); static String getRealClientIpAddr(final HttpServletRequest req); static void reportLoginEvent(final EventType eventType, final String username,
      final String ip, final boolean isSuccess, final String message); static void reportLoginEvent(final EventType eventType, final String username, final String ip); static final String X_FORWARDED_FOR_HEADER; }
@Test public void testWhenNoXForwardedForHeaderUseClientIp() { final String clientIp = "127.0.0.1:10000"; final Map<String, String> headers = new HashMap<>(); final String ip = WebUtils.getRealClientIpAddr(headers, clientIp); assertEquals(ip, "127.0.0.1"); }
@Test public void testWhenClientIpNoPort() { final String clientIp = "192.168.1.1"; final Map<String, String> headers = new HashMap<>(); final String ip = WebUtils.getRealClientIpAddr(headers, clientIp); assertEquals(ip, "192.168.1.1"); }
@Test public void testWhenXForwardedForHeaderUseHeader() { final String clientIp = "127.0.0.1:10000"; final String upstreamIp = "192.168.1.1:10000"; final Map<String, String> headers = new HashMap<>(); headers.put("X-Forwarded-For", upstreamIp); final String ip = WebUtils.getRealClientIpAddr(headers, clientIp); assertEquals(ip, "192.168.1.1"); }
@Test public void testWhenXForwardedForHeaderMultipleUpstreamsUseHeader() { final String clientIp = "127.0.0.1:10000"; final String upstreamIp = "192.168.1.1:10000"; final Map<String, String> headers = new HashMap<>(); headers.put("X-Forwarded-For", upstreamIp + ",127.0.0.1,55.55.55.55"); final String ip = WebUtils.getRealClientIpAddr(headers, clientIp); assertEquals(ip, "192.168.1.1"); }
@Test @Ignore public void testIPv6NoPort() { final String clientIp = "fe80::aede:48ff:.55805"; final Map<String, String> headers = new HashMap<>(); final String ip = WebUtils.getRealClientIpAddr(headers, clientIp); assertEquals(ip, "fe80::aede:48ff"); }