通过request对象获取客户端头文件请求信息,分析出请求客户端的ip地址信息(包括使用了Apache,Nginx等反向代理软件情况),工具类HttpClientIpUtils源码如下
import javax.servlet.http.HttpServletRequest;@b@@b@public class HttpClientIpUtils@b@{@b@ public static String getIp(HttpServletRequest request)@b@ {@b@ String ip = request.getHeader("x-forwarded-for");//配置代理情况,配置代理后header信息通过x-forwarded-for标记真实ip地址@b@@b@ if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {@b@ ip = request.getHeader("Proxy-Client-IP");//整合apache+Weblogic 代理情况@b@ }@b@@b@ if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {@b@ ip = request.getHeader("WL-Proxy-Client-IP");//整合apache+Weblogic 代理情况@b@ }@b@@b@ if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {@b@ ip = request.getRemoteAddr();//获取未配置代理的情况@b@ }@b@@b@ ip = getIp(ip);@b@ return ip;@b@ }@b@@b@ private static String getIp(String ip)@b@ {@b@ if ((ip != null) && (ip.indexOf(44) > 0)) {@b@ String[] ipArray = ip.split(",");@b@@b@ ip = ipArray[(ipArray.length - 1)].trim();@b@ }@b@ return ip;@b@ }@b@}