@Test public void testIgnoreForwardedFor() { when(request.getRemoteAddr()).thenReturn("9.9.9.9"); when(request.getHeaders("X-FORWARDED-FOR")).thenReturn(Collections.enumeration(ImmutableList.of("1.1.1.1, 2.2.2.2", "3.3.3.3, 4.4.4.4"))); assertEquals(new ClientAddressExtractor().clientAddressFor(request), "9.9.9.9"); }
public String clientAddressFor(HttpServletRequest request) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (Enumeration<String> e = request.getHeaders("X-FORWARDED-FOR"); e != null && e.hasMoreElements(); ) { String forwardedFor = e.nextElement(); builder.addAll(Splitter.on(',').trimResults().omitEmptyStrings().split(forwardedFor)); } if (request.getRemoteAddr() != null) { builder.add(request.getRemoteAddr()); } String clientAddress = null; ImmutableList<String> clientAddresses = builder.build(); for (String address : Lists.reverse(clientAddresses)) { try { if (!trustedNetworks.containsAddress(InetAddresses.forString(address))) { clientAddress = address; break; } clientAddress = address; } catch (IllegalArgumentException ignored) { break; } } if (clientAddress == null) { clientAddress = request.getRemoteAddr(); } return clientAddress; }
ClientAddressExtractor { public String clientAddressFor(HttpServletRequest request) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (Enumeration<String> e = request.getHeaders("X-FORWARDED-FOR"); e != null && e.hasMoreElements(); ) { String forwardedFor = e.nextElement(); builder.addAll(Splitter.on(',').trimResults().omitEmptyStrings().split(forwardedFor)); } if (request.getRemoteAddr() != null) { builder.add(request.getRemoteAddr()); } String clientAddress = null; ImmutableList<String> clientAddresses = builder.build(); for (String address : Lists.reverse(clientAddresses)) { try { if (!trustedNetworks.containsAddress(InetAddresses.forString(address))) { clientAddress = address; break; } clientAddress = address; } catch (IllegalArgumentException ignored) { break; } } if (clientAddress == null) { clientAddress = request.getRemoteAddr(); } return clientAddress; } }
ClientAddressExtractor { public String clientAddressFor(HttpServletRequest request) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (Enumeration<String> e = request.getHeaders("X-FORWARDED-FOR"); e != null && e.hasMoreElements(); ) { String forwardedFor = e.nextElement(); builder.addAll(Splitter.on(',').trimResults().omitEmptyStrings().split(forwardedFor)); } if (request.getRemoteAddr() != null) { builder.add(request.getRemoteAddr()); } String clientAddress = null; ImmutableList<String> clientAddresses = builder.build(); for (String address : Lists.reverse(clientAddresses)) { try { if (!trustedNetworks.containsAddress(InetAddresses.forString(address))) { clientAddress = address; break; } clientAddress = address; } catch (IllegalArgumentException ignored) { break; } } if (clientAddress == null) { clientAddress = request.getRemoteAddr(); } return clientAddress; } ClientAddressExtractor(); @Inject  ClientAddressExtractor(InternalNetworkConfig config); }
ClientAddressExtractor { public String clientAddressFor(HttpServletRequest request) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (Enumeration<String> e = request.getHeaders("X-FORWARDED-FOR"); e != null && e.hasMoreElements(); ) { String forwardedFor = e.nextElement(); builder.addAll(Splitter.on(',').trimResults().omitEmptyStrings().split(forwardedFor)); } if (request.getRemoteAddr() != null) { builder.add(request.getRemoteAddr()); } String clientAddress = null; ImmutableList<String> clientAddresses = builder.build(); for (String address : Lists.reverse(clientAddresses)) { try { if (!trustedNetworks.containsAddress(InetAddresses.forString(address))) { clientAddress = address; break; } clientAddress = address; } catch (IllegalArgumentException ignored) { break; } } if (clientAddress == null) { clientAddress = request.getRemoteAddr(); } return clientAddress; } ClientAddressExtractor(); @Inject  ClientAddressExtractor(InternalNetworkConfig config); String clientAddressFor(HttpServletRequest request); }
ClientAddressExtractor { public String clientAddressFor(HttpServletRequest request) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (Enumeration<String> e = request.getHeaders("X-FORWARDED-FOR"); e != null && e.hasMoreElements(); ) { String forwardedFor = e.nextElement(); builder.addAll(Splitter.on(',').trimResults().omitEmptyStrings().split(forwardedFor)); } if (request.getRemoteAddr() != null) { builder.add(request.getRemoteAddr()); } String clientAddress = null; ImmutableList<String> clientAddresses = builder.build(); for (String address : Lists.reverse(clientAddresses)) { try { if (!trustedNetworks.containsAddress(InetAddresses.forString(address))) { clientAddress = address; break; } clientAddress = address; } catch (IllegalArgumentException ignored) { break; } } if (clientAddress == null) { clientAddress = request.getRemoteAddr(); } return clientAddress; } ClientAddressExtractor(); @Inject  ClientAddressExtractor(InternalNetworkConfig config); String clientAddressFor(HttpServletRequest request);  }