java httpRequest getRemoteAddr始终返回127.0.0.1 [英] java httpRequest getRemoteAddr allways return 127.0.0.1

查看:758
本文介绍了java httpRequest getRemoteAddr始终返回127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,谢谢您的帮助,

Hello everybody and thanks for your help,

我正在使用JSF开发Web应用程序,出于安全原因,我需要在用户登录时捕获其IP.

I am developing a web application using JSF and for security reasons i need to capture the user's IP when he does the log in.

我正在使用以下代码:

HttpServletRequest request=(HttpServletRequest) context.getExternalContext().getRequest();
remoteAddress=request.getRemoteAddr();

问题是,当我检查我的应用程序日志以检查ip地址时,它总是返回127.0.0.1.

The thing is that when I check my app logs to check the ip address it always returns 127.0.0.1.

[INFO] 2014年7月1日11:04:22->从127.0.0.1连接的用户xxx

[INFO] 07/01/2014 11:04:22 --> User xxx connected from 127.0.0.1

[INFO] 2014年7月1日11:27:43->从127.0.0.1连接的用户xxx

[INFO] 07/01/2014 11:27:43 --> User xxx connected from 127.0.0.1

更多详细信息:

  • 我正在使用tomcat 7
  • 我的服务器在debian虚拟机中工作
  • 我正在从另一台包含VM的PC连接到服务器.

我已经在互联网上搜索并阅读了有关使用的信息:

I have searched in internet and read about using:

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

但是它并没有解决我的问题.

But It did not solved my problem.

此刻我很失落,有什么想法为什么会发生吗?

I am quite lost at the moment, any ideas why is this happening?

推荐答案

首先,检查您的http服务器是否应该转发标头.您正在使用nginx,这应该可以工作

First of all, check if your http server should be forwarding the headers. You you're using nginx, this should work

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

如果您已经完成此操作,则可以使用Tomcat的RemoteIpValve

If you're already done that, you can accomplish that using Tomcat's RemoteIpValve

<Valve className="org.apache.catalina.valves.RemoteIpValve"
           remoteIpHeader="X-Forwarded-For"
           requestAttributesEnabled="true"
           internalProxies="127.0.0.1"  />

这样,当您致电request.getRemoteAddr()时,它将提供正确的信息.

That way, when you call request.getRemoteAddr(), it will be providing the right information.

这篇关于java httpRequest getRemoteAddr始终返回127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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