Am using Spring 4 MVC for RESTful Web Services on Tomcat 7.
Was wondering if an external client sends a HTTP request, is there a way to obtain the particular client's IP Address within the server side layer?
Am using Spring 4 MVC for RESTful Web Services on Tomcat 7.
Was wondering if an external client sends a HTTP request, is there a way to obtain the particular client's IP Address within the server side layer?
org.springframework.security.web.authentication.WebAuthenticationDetails
A holder of selected HTTP details related to a web authentication request.
Indicates the TCP/IP address the authentication request was received from.
String ipAddress = ((WebAuthenticationDetails)SecurityContextHolder.getContext().getAuthentication().getDetails()).getRemoteAddress();
If you are testing on a local application, your ip address will be "0:0:0:0:0:0:0:1"
Simple POJO
public static InetAddress getClientIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
try {
return InetAddress.getByName(ip);
} catch (UnknownHostException e) {
return null;
}
}