發(fā)生的場景:服務器端接收客戶端請求的時候,一般需要進行簽名驗證,客戶端IP限定等情況,在進行客戶端IP限定的時候,需要首先獲取該真實的IP。一般分為兩種情況:
方式一、客戶端未經(jīng)過代理,直接訪問服務器端(nginx,squid,haproxy);
方式二、客戶端通過多級代理,最終到達服務器端(nginx,squid,haproxy);
客戶端請求信息都包含在HttpServletRequest中,可以通過方法getRemoteAddr()獲得該客戶端IP。此時如果在使用方式一形式,可以直接獲得該客戶端真實IP。而如果是方式二中通過代理的形式,此時經(jīng)過多級反向的代理,通過方法getRemoteAddr()得不到客戶端真實IP,可以通過x-forwarded-for獲得轉發(fā)后請求信息。當客戶端請求被轉發(fā),IP將會追加在其后并以逗號隔開,例如:10.47.103.13,4.2.2.2,10.96.112.230。
請求中的參數(shù):
request.getHeader("x-forwarded-for") : 10.47.103.13,4.2.2.2,10.96.112.230
request.getHeader("X-Real-IP") : 10.47.103.13
request.getRemoteAddr():10.96.112.230
客戶端訪問經(jīng)過轉發(fā),IP將會追加在其后并以逗號隔開。最終準確的客戶端信息為:
代碼示例:
/** * 獲取用戶真實IP地址,不使用request.getRemoteAddr()的原因是有可能用戶使用了代理軟件方式避免真實IP地址, * 可是,如果通過了多級反向代理的話,X-Forwarded-For的值并不止一個,而是一串IP值 * * @return ip */ private String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); System.out.println("x-forwarded-for ip: " + ip); if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { // 多次反向代理后會有多個ip值,第一個ip才是真實ip if( ip.indexOf(",")!=-1 ){ ip = ip.split(",")[0]; } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); System.out.println("Proxy-Client-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); System.out.println("WL-Proxy-Client-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); System.out.println("HTTP_CLIENT_IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); System.out.println("X-Real-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); System.out.println("getRemoteAddr ip: " + ip); } System.out.println("獲取客戶端ip: " + ip); return ip; }
此時,正常情況之下可以獲取客戶端真實的IP。需要注意的是對于服務器端采用負載的形式,需要配置保存x-forwarded-for。目前負載的形式有haproxy、nginx等形式。結構圖如下:
我接觸的服務器端配置負載使用haproxy,相對于的配置如下:
frontend crmtest bind :8081 mode http option httplog option forwardfor // 配置HAProxy會把客戶端的IP信息發(fā)送給服務器,在HTTP請求中添加"X-Forwarded-For"字段。 default_backend drmbackend drm mode http option httplog balance roundrobin stick-table type ip size 200k expire 30m stick on src server jboss1 10.10.10.11:8081 server jboss2 10.10.10.12:8081
haproxy配置參考:http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html