java 如果使用“localhost” URL,则无法从 HttpServletRequest 获取 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14180416/
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-10-31 15:22:46  来源:igfitidea点击:

Can't get IP from HttpServletRequest if using "localhost" URL

javajakarta-eeservlets

提问by Chailie

I got a problem when trying to get IP from HttpServletRequest,Please see my coding first:

尝试从 HttpServletRequest 获取 IP 时遇到问题,请先查看我的编码:

String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip; 

My problem is if opened application with following URL (My PC's URL is 18.111 and the server deployed on localhost) "https://192.168.18.111:8443/test/main.html",I could get right URL with above coding,But if opened with "https://localhost:8443/test/main.html",It would return something like "0.1.0.1...." with above function,Why this function didn't work for "localhost" or does anybody know if have any better way to get IP from HttpServletRequest?

我的问题是,如果使用以下 URL 打开应用程序(我的 PC 的 URL 是 18.111 并且服务器部署在本地主机上)“https://192.168.18.111:8443/test/main.html”,我可以通过上述编码获得正确的 URL,但是如果用“https://localhost:8443/test/main.html”打开,它会返回类似“0.1.0.1....”的东西,上面的功能,为什么这个功能对“localhost”不起作用或有人知道是否有更好的方法从 HttpServletRequest 获取 IP?

回答by Kapelchik

Your method's result is absolutely correct. I assume the number that you're getting is 0:0:0:0:0:0:0:1. It's valid form of loopback address. But it's just localhost in IPv6 format. IPv4 address of localhost is 127.0.0.1and IPv6 address of localhost is 0:0:0:0:0:0:0:1.

你的方法的结果是绝对正确的。我假设你得到的数字是0:0:0:0:0:0:0:1。它是环回地址的有效形式。但它只是 IPv6 格式的本地主机。本地主机的 IPv4 地址为 ,本地主机的127.0.0.1IPv6 地址为0:0:0:0:0:0:0:1

The thing's that the URL https://localhost:8443/test/main.htmlmatches by default both versions of IP protocol. Apparently your browser chooses to use IPv6.

问题是 URLhttps://localhost:8443/test/main.html默认匹配两个版本的 IP 协议。显然您的浏览器选择使用 IPv6。

For local testing try using literal address 127.0.0.1instead of name localhost. Or you can make have only IPv4 address in your DNS settings.

对于本地测试,尝试使用文字 address127.0.0.1而不是 name localhost。或者,您可以在 DNS 设置中只设置 IPv4 地址。

回答by Ariel Chels?u

Why not trying

为什么不试试

request.getRemoteAddr()

only? Do you really need the originating IP address (the one provided by "X-Forwarded-For")? I'm guessing that sometimes it's not useful to see some dumb LAN address instead of an addressable resource - provided by the upper method.

只要?您真的需要原始 IP 地址(“X-Forwarded-For”提供的地址)吗?我猜有时看到一些愚蠢的 LAN 地址而不是可寻址资源是没有用的 - 由上层方法提供。

Later edit:

后期编辑:

Have a look at this question: Finding user ip addresswhich seems to be fixed.

看看这个问题:Finding user ip addresswhich似乎是固定的。