How do you get Client IP Address in Grails controller?

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2010-01-26T16:37:29Z Indexed on 2010/05/23 11:20 UTC
Read the original article Hit count: 535

Filed under:

I had code like this in Ruby

@clientipaddress = request.env["HTTP_CLIENT_IP"]
if (@clientipaddress == nil)
  @clientipaddress = request.env["HTTP_X_FORWARDED_FOR"]
end
if (@clientipaddress == nil)
  @clientipaddress = request.env["REMOTE_ADDR"]
end
if (@clientipaddress != nil)
  comma = @clientipaddress.index(",")
  if (comma != nil && comma >= 0)
    @clientipaddress = @clientipaddress[0, comma]
  end
end

It took care of all the possible ways that the ip might show up. For instance, on my local development machine there is no proxy. But in QA and Production the proxies are there and sometimes they provide more than one address.

I don't need to know the groovy syntax. Just which methods get me the equivalent of the three different ways I ask for the ip above.

© Stack Overflow or respective owner

Related posts about grails