Try following code to get client IP address.
// headers in order of trust, most trusted at top
Then in your JSP you can just do
<%=getClientIpAddr(slingRequest)%>
Note: Make sure that all headers are allowed from dispatcher
At dispatcher side make sure that you are allowing all the headers with this configuration in dispatcher.any
/clientheaders
{
"*"
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum IPHeaders { | |
CLIENT_IP_UNDERSCORE("Client_IP"), | |
CLIENT_IP("Client-IP"), | |
X_CLIENT_IP("X-Client-IP"), | |
X_FORWARDED_FOR("X-Forwarded-For"), | |
FORWARDED_FOR("Forwarded-For"), | |
VIA("Via"), | |
REMOTE_ADDR("Remote-Addr"), | |
X_REMOTE_ADDR("X-Remote-Addr"), | |
X_CLUSTER_CLIENT_IP("X-Cluster-Client-IP"), | |
X_FORWARDED("X-Forwarded"), | |
FORWARDED("Forwarded"); | |
public final String text; | |
private IPHeaders(String text) | |
{ | |
this.text = text; | |
} | |
public String getText() | |
{ | |
return text; | |
} | |
}; | |
//And Then | |
public String getClientIpAddr(SlingHttpServletRequest request) { | |
String ip = ""; | |
// looks through http headers, case-insensitive | |
for (IPHeaders ipHeader : IPHeaders.values()) { | |
ip = StringEscapeUtils.escapeHtml4(request.getHeader(ipHeader.getText())); | |
if (ip != null) { | |
LOGGER.debug(ipHeader.getText() + "=" + ip); | |
} | |
if (ip != null && ip.length()>0 && !"unknown".equalsIgnoreCase(ip)) { | |
return ip; | |
} | |
} | |
// note that if headers aren't set, we could return source of immediate upstream request | |
// using request.getRemoteAddr(), but this isn't meaningful for us | |
String remoteAddr = request.getRemoteAddr(); | |
if (remoteAddr != null) { | |
LOGGER.debug("getRemoteAddr=" + remoteAddr); | |
ip = remoteAddr; | |
} | |
if (ip == null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { | |
ip = "0.0.0.0"; // send a dummy value | |
} | |
return ip; | |
} |
Then in your JSP you can just do
<%=getClientIpAddr(slingRequest)%>
Note: Make sure that all headers are allowed from dispatcher
At dispatcher side make sure that you are allowing all the headers with this configuration in dispatcher.any
/clientheaders
{
"*"
}
Hi,
ReplyDeleteCan you please share what configuration we have to do in dispatcher?
Sujeet,
DeleteFor this just make sure that all the headers are enabled. will update blog post.
Yogesh
String ipAddress = request.getHeader("X-FORWARDED-FOR");
ReplyDeleteif (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
When we use the above code it x-forwarded-for is returning null and getremoteaddr() is returning the server or loadbalancer Ip.could you please let me know what could be the issue?
Hello Prakash,
DeleteYou have to make sure that your LB supports x-forwarded-for header. If not then you have to work with them to find out which header contains client IP. In some cases client_ip is that header. I am changing code to see all possible header that contains client IP.
Yogesh
thanks
ReplyDeleteCool! Thanks for all this info and i'm sure that you will be happy to know that i've used one of this top vpn services and recommend it to you
ReplyDelete