尝试从 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;
我的问题是如果使用以下 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?