为什么 HttpServletRequest.getRemoteAddr() 在 Java servlet 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/262887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:11:34  来源:igfitidea点击:

Why HttpServletRequest.getRemoteAddr() doesn't work in Java servlet?

javahtmlservlets

提问by Frank

I'm developing a web app with java servlet, I hope to get the user ip info by calling request.getRemoteAddr()from inside processRequest(HttpServletRequest request,HttpServletResponse response).

我正在使用 java servlet 开发一个 web 应用程序,我希望通过request.getRemoteAddr()从内部调用来获取用户 ip 信息processRequest(HttpServletRequest request,HttpServletResponse response)

But it returns a wrong IP. Since I'm not very knowledgeable about this area, I don't know what it is displaying, maybe a proxy, I got this:

但它返回错误的IP。由于我对这个领域不是很了解,我不知道它显示的是什么,也许是一个代理,我得到了这个:

RemoteAddr : 127.0.0.1
RemoteHost : 127.0.0.1
x-forwarded-for : null

127.0.0.1 is not my IP.

127.0.0.1 不是我的 IP。

Yet when I go to: http://www.javascriptkit.com/script/script2/displayip.shtmlit will display the right one, since I'm using servlet, I don't have the .shtml to my dynamically generated html page, what can I do? And why the script on that site can display it correctly while request.getRemoteAddr()can't do it?

然而,当我去:http://www.javascriptkit.com/script/script2/displayip.shtml它会显示正确的,因为我使用 servlet,我没有 .shtml 到我的动态生成的 html 页面,我该怎么办?为什么那个网站上的脚本可以正确显示而request.getRemoteAddr()不能正确显示?

Thanks for all the answers, I have a clue now, after deploying it to the server, it works as expected. Showed the correct address.

感谢所有的答案,我现在有一个线索,将它部署到服务器后,它按预期工作。显示正确的地址。

But even when I develop it on my local machine, how to ask it to display the absolute IP as if it running on a real server? Or is it doable?

但是即使我在本地机器上开发它,如何要求它像在真实服务器上运行一样显示绝对IP?或者它可行吗?

回答by Jon Skeet

What IP address is it displaying? My guess is there's some proxy or something changing things. (For instance, that script page displayed my ADSL router's IP address - not the one inside my LAN - for obvious reasons.)

它显示的IP地址是什么?我的猜测是有一些代理或一些改变的东西。(例如,出于显而易见的原因,该脚本页面显示了我的 ADSL 路由器的 IP 地址——而不是我局域网内的 IP 地址。)

EDIT: Now that you've shown that the IP address you're seeing is 127.0.0.1 the answer is fairly clear - you're seeing your loopback adapter (i.e. the shortcut to the same machine) presumably because you're testing on the same machine you're developing on. The answer is entirely correct.

编辑:既然你已经证明你看到的 IP 地址是 127.0.0.1,答案就很清楚了——你看到的是环回适配器(即同一台机器的快捷方式),大概是因为你正在测试您正在开发的同一台机器。答案是完全正确的。

Try it from a different machine and you'll get a more useful IP address.

在另一台机器上尝试,你会得到一个更有用的 IP 地址。

回答by Vijay Dev

Check the X-Forwarded-For header by calling request.getHeader("X-Forwarded-For") and see what IP does it return.

通过调用 request.getHeader("X-Forwarded-For") 检查 X-Forwarded-For 标头并查看它返回的 IP。

回答by Huntrods

The returned IP that you are showing is the localhost IP. This raises the question - where are you testing, and how are you accessing the servlet to test?

您显示的返回 IP 是本地主机 IP。这就提出了一个问题——你在哪里测试,你如何访问 servlet 进行测试?

If you are running the servlet on your local (development) machine, and also calling it up from a browser on the same machine, then this output is absolutely correct.

如果您在本地(开发)机器上运行 servlet,并从同一台机器上的浏览器调用它,那么这个输出是绝对正确的。

Cheers,

干杯,

-R

-R

回答by masto

You're running your test server on your local computer and connecting to it on http://localhost/. Since you're connecting on the local interface, the source of the connection is also localhost, aka 127.0.0.1.

您正在本地计算机上运行测试服务器并通过http://localhost/连接到它。由于您在本地接口上进行连接,因此连接源也是 localhost,也就是 127.0.0.1。

回答by mfx

If you call your servlet using http://localhost:8080/servlet, you will usually get "localhost" as the remote addr. If you use the name of your machine, i.e. http://yourmachine/servlet, you will usally get the "correct" address.

如果您使用http://localhost:8080/servlet调用您的 servlet ,您通常会得到“localhost”作为远程地址。如果您使用机器的名称,即http://yourmachine/servlet,您通常会得到“正确”的地址。