How do you get the client IP address in a Grails controller?

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 

He took care of all the possible paths that IP could show. For example, there is no proxy on my local development machine. But QA and Production have proxies, and sometimes they provide more than one address.

I don't need to know Groovy syntax, only which methods give me the equivalent of the three different ways that I request for IP above.

+12
source share
1 answer

I think this should be what you want:

  • request.getRemoteAddr()
  • request.getHeader("X-Forwarded-For")
  • request.getHeader("Client-IP")
+34
source

Source: https://habr.com/ru/post/1381283/


All Articles