/** * 得到用戶遠程地址 */ private String getRemoteAddr(HttpServletRequest request) { String remoteAddr = request.getHeader("X-Real-IP"); if (StringUtils.isEmpty(remoteAddr)) { remoteAddr = request.getHeader("X-Forwarded-For"); } else if (StringUtils.isEmpty(remoteAddr)) { remoteAddr = request.getHeader("Proxy-Client-IP"); } else if (StringUtils.isEmpty(remoteAddr)) { remoteAddr = request.getHeader("WL-Proxy-Client-IP"); } return remoteAddr != null ? remoteAddr : request.getRemoteAddr(); }
多級反向代理時狀況可能更復雜。 參考:(http://www.oschina.net/question/819166_124476) (http://blog.csdn.net/sgx425021234/article/details/19043459)java