Friday, September 30, 2011

How to get Client IP through dispatcher in CQ

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

6 comments:

  1. Hi,
    Can you please share what configuration we have to do in dispatcher?

    ReplyDelete
    Replies
    1. Sujeet,

      For this just make sure that all the headers are enabled. will update blog post.

      Yogesh

      Delete
  2. String ipAddress = request.getHeader("X-FORWARDED-FOR");
    if (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?

    ReplyDelete
    Replies
    1. Hello Prakash,

      You 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

      Delete
  3. Cool! 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