First, on the code, in the explanation behind, the code is as follows
Private static String getIpAddress(HttpServletRequest Request) {String Xip = request.getHeader("X-Real-IP"); String XFor = request.getHeader("X-Forwarded-For"); if(StringUtils.isNotEmpty(XFor) && !" UnKnown ".equalsignoRecase (XFor)){// After multiple reverse proxies there are multiple IP addresses, the first IP address is the real IP address int index = xfor.indexof (","); if(index ! = -1){ return XFor.substring(0,index); }else{ return XFor; } } XFor = Xip; if(StringUtils.isNotEmpty(XFor) && !" unKnown".equalsIgnoreCase(XFor)){ return XFor; } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("HTTP_CLIENT_IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getRemoteAddr(); }}Copy the code
Second, code interpretation
First, we get the 0th bit of the X-Forwarded-For IP address, which is the actual client IP that can be represented by the HTTP extension protocol. It goes something like this:
X-forwarded-for: Client, proxy1, proxy2, proxy…
So you can see why it’s the 0th bit! If YOU don’t get it from X-Forwarded-For, you get it from X-Real-IP. If you don’t get it from X-Forwarded-For, you get it from X-Real-IP. Get proxy-client-IP, wL-proxy-client-ip, HTTP_CLIENT_IP, and HTTP_X_FORWARDED_FOR in sequence. If no, request. GetRemoteAddr () is used to obtain the IP address. X-real-ip is the Real IP address of the client that records the request. It’s kind of like X-Forwarded-For.
Proxy-client-ip is the IP address of the Proxy Client. If the real IP address of the Proxy Client cannot be obtained, you can only obtain the IP address of the Proxy Client.
Wl-proxy-client-ip is a parameter used to obtain the real IP address in Weblogic.
HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR can be read as X-Forwarded-for because they are used in PHP.