Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get Client IP Address in a Grails controller?

Tags:

grails

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.

like image 731
Andrew Avatar asked Jan 26 '10 16:01

Andrew


1 Answers

I think this should be what you want:

  • request.getRemoteAddr()
  • request.getHeader("X-Forwarded-For")
  • request.getHeader("Client-IP")
like image 191
Michael Borgwardt Avatar answered Nov 06 '22 22:11

Michael Borgwardt