I have a Java Spring application that uses a REST based webservice. This service has an endpoint that should capture the IP address of the request, and send it to my JSP to collect some info. I've tried all methods from SO, and I always seem to get the same result, which is the IP address of the host server. The relevant Java class is:
private static final String[] IP_HEADER_CANDIDATES = {
"X-Forwarded-For",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"HTTP_X_FORWARDED_FOR",
"HTTP_X_FORWARDED",
"HTTP_X_CLUSTER_CLIENT_IP",
"HTTP_CLIENT_IP",
"HTTP_FORWARDED_FOR",
"HTTP_FORWARDED",
"HTTP_VIA",
"REMOTE_ADDR" };
public void doStuff(HttpServletRequest servletRequest, ...) {
//...
String ipAddress = getClientIpAddress(servletRequest);
servletRequest.setAttribute("ip", ipAddress);
//...
}
public static String getClientIpAddress(HttpServletRequest request) {
for (String header : IP_HEADER_CANDIDATES) {
String ip = request.getHeader(header);
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
}
return request.getRemoteAddr();
}
When I forward to my JSP and call console.log("${ip}");
, I'm always seeing the IP of docker which my app is running on (basically localhost). When I deployed my code to a deployment server, the IP address I'm seeing did not reflect the IP address in my ifconfig (I'm testing this endpoint on a company VPN, thought shouldn't this code detect any proxies?). Am I not capturing the IP correctly?