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

查看:26
本文介绍了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 网络服务器.谢谢!

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

推荐答案

您可以看穿"代理并使用X-FORWARDED-FOR 标头从X-FORWARDED-FOR 标头中获取原始请求者的地址/p>

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天全站免登陆