在我的Web应用程序中,我需要为代理后面的客户端提取真实IP地址,经过搜索我发现可能的方法是读取“X_FORWARDED_FOR”标头的内容,我正在使用java servlet和传入的标头请求不包含“X_FORWARDED_FOR”标头,那么为什么标头不包含在请求中?
我正在使用 java 1.7、tomcat v7、代理服务器:TMG 并配置为使用“X_FORWARDED_FOR”标头。
请指教。
提前致谢。
更新 :
- 该请求来自对 servlet 的 jquery ajax 请求。
我读取可用标题的代码:
String ip = request.getHeader("X-Forwarded-For");
//返回空if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); //return null } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); //return null } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); //return null } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); //return null } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); //return proxy server IP }