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

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

问题描述

我在Ruby中有这样的代码:

  @clientipaddress = request.env [HTTP_CLIENT_IP] 
(@clientipaddress == nil)
@clientipaddress = request.env [HTTP_X_FORWARDED_FOR]
结束
if(@clientipaddress ==零)
@clientipaddress = request.env [REMOTE_ADDR]
结束
if(@ clientipaddress!= nil)
comma = @ clientipaddress.index(,)
if(comma!= nil&& amp; ;逗号> = 0)
@clientipaddress = @clientipaddress [0,逗号]
结束
结束

它照顾了知识产权可能出现的所有可能的方式。例如,在我的本地开发机器上,没有代理。但在QA和Production中,代理服务器就在那里,有时他们提供了多个地址。



我不需要知道Groovy语法,只需要哪些方法让我相当于我要求上述IP的三种不同方式。 我认为这应该是你想要的:




  • request.getRemoteAddr()

  • request.getHeader(X-Forwarded-For)

  • request.getHeader(Client-IP )


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.

解决方案

I think this should be what you want:

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

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

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