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
{
"*"
}
// 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
{
"*"
}
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