項目中須要和第三方平臺接口,加了來源IP鑑權功能,測試時發現沒有問題,可是部署之後發現存在問題,一直鑑權不經過,一羣人抓瞎。java
我找到那塊的代碼,跟了一遍流程發現邏輯沒有啥問題,可是最終的結果卻仍是鑑權不經過,實在有些詭異。其基本邏輯爲先取得配置的IP列表,而後經過request.getRemoteAddr()取得客戶端的IP地址,作鑑權和校驗,邏輯沒問題,那麼確定是request.getRemoteAddr()出了問題,google下,發現有人遇到相似的問題。web
最終定位爲request.getRemoteAddr()這種方法在大部分狀況下都是有效的。可是在經過了Apache,Squid等反向代理軟件就不能獲取到客戶端的真實IP地址了。apache
若是使用了反向代理軟件,將http://192.168.1.110:2046/ 的URL反向代理爲http://www.xxx.com/ 的URL時,用request.getRemoteAddr()方法獲取的IP地址是:127.0.0.1 或 192.168.1.110,而並非客戶端的真實IP。後端
通過代理之後,因爲在客戶端和服務之間增長了中間層,所以服務器沒法直接拿到客戶端的IP,服務器端應用也沒法直接經過轉發請求的地址返回給客戶端。可是在轉發請求的HTTP頭信息中,增長了X-FORWARDED-FOR信息用以跟蹤原有的客戶端IP地址和原來客戶端請求的服務器地址。服務器
原來如此,咱們的項目中正好是有前置apache,將一些請求轉發給後端的weblogic,看來就是這樣致使的咯。函數
給出一份還算靠譜的代碼測試
改進後的java代碼:ui
class IpUtils { public static String getIpAddr(HttpServletRequest request) { 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; } }
PS:但是,若是經過了多級反向代理的話,X-Forwarded-For的值並不止一個,而是一串ip值,究竟哪一個纔是真正的用戶端的真實IP呢?
答案是取X-Forwarded-For中第一個非unknown的有效IP字符串。如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100,用戶真實IP爲: 192.168.1.110google
================================下面是OSC中的=====================================spa
import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; public final class InternetProtocol { private static Logger log = Logger.getLogger(InternetProtocol.class); /** * 構造函數. */ private InternetProtocol() { } /** * 獲取客戶端IP地址.<br> * 支持多級反向代理 * * @param request * HttpServletRequest * @return 客戶端真實IP地址 */ public static String getRemoteAddr(final HttpServletRequest request) { try{ String remoteAddr = request.getHeader("X-Forwarded-For"); // 若是經過多級反向代理,X-Forwarded-For的值不止一個,而是一串用逗號分隔的IP值,此時取X-Forwarded-For中第一個非unknown的有效IP字符串 if (isEffective(remoteAddr) && (remoteAddr.indexOf(",") > -1)) { String[] array = remoteAddr.split(","); for (String element : array) { if (isEffective(element)) { remoteAddr = element; break; } } } if (!isEffective(remoteAddr)) { remoteAddr = request.getHeader("X-Real-IP"); } if (!isEffective(remoteAddr)) { remoteAddr = request.getRemoteAddr(); } return remoteAddr; }catch(Exception e){ log.error("get romote ip error,error message:"+e.getMessage()); return ""; } } /** * 獲取客戶端源端口 * @param request * @return */ public static Long getRemotePort(final HttpServletRequest request){ try{ String port = request.getHeader("remote-port"); if( StringUtil.isNotEmptyString(port )) { try{ return Long.parseLong(port); }catch(NumberFormatException ex){ log.error("convert port to long error , port: "+port); return 0l; } }else{ return 0l; } }catch(Exception e){ log.error("get romote port error,error message:"+e.getMessage()); return 0l; } } /** * 遠程地址是否有效. * * @param remoteAddr * 遠程地址 * @return true表明遠程地址有效,false表明遠程地址無效 */ private static boolean isEffective(final String remoteAddr) { boolean isEffective = false; if ((null != remoteAddr) && (!"".equals(remoteAddr.trim())) && (!"unknown".equalsIgnoreCase(remoteAddr.trim()))) { isEffective = true; } return isEffective; } }