如何在 Grails 控制器中获取客户端 IP 地址? [英] How do you get Client IP Address in a Grails controller?

查看:24
本文介绍了如何在 Grails 控制器中获取客户端 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ruby 中有这样的代码:

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

它处理了 IP 可能出现的所有可能方式.例如,在我的本地开发机器上,没有代理.但在 QA 和生产中,代理就在那里,有时它们提供不止一个地址.

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.

我不需要知道 Groovy 语法,只需要知道哪种方法就可以让我获得上述三种不同的 IP 请求方式.

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.

推荐答案

我认为这应该是你想要的:

I think this should be what you want:

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

这篇关于如何在 Grails 控制器中获取客户端 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆