HttpServletRequest.getRemoteAddr()返回错误的地址 [英] HttpServletRequest.getRemoteAddr() returning wrong address

查看:182
本文介绍了HttpServletRequest.getRemoteAddr()返回错误的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要通过Seam操作记录客户端的IP地址.目前,我们正在使用以下代码:

We need to log the client's IP address from a Seam action. Currently, we're using the code:

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest)context.getRequest();

this.remoteAddress =  request.getRemoteAddr();

但是,这似乎总是返回我们网络的内部地址,而不是客户端的IP地址.根据我的研究,似乎在网络中拥有反向代理可能会造成混乱,但是我们可以通过重新配置Web服务器来修复它.还有其他人遇到过这个问题,您是如何解决的?

However, this seems to be always returning an internal address of our network, rather than the client's IP address. From my research, it seems that having a reverse proxy in the network can confuse things, but we might be able to fix it by reconfiguring our web servers. Has anyone else had this problem, and how did you solve it?

我们正在使用JBoss 5.1应用服务器和Apache Web服务器.谢谢!

We're using JBoss 5.1 app servers and Apache web servers. Thanks!

推荐答案

您可以查看"代理,并使用

X-FORWARDED-FOR标头中获取原始请求者的地址.

You can 'see through' a proxy and get the address of the original requestor from the X-FORWARDED-FOR header using

request.getHeader( "X-FORWARDED-FOR" );

我想原始请求者和您的服务器之间的代理行为不当可能会导致丢失真实值.

I guess a misbehaving proxy between the original requestor and your server could result in the true value being lost.

参考: X-FORWARDED-FOR

这篇关于HttpServletRequest.getRemoteAddr()返回错误的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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