参考:https://www.jb51.net/article/243543.htm
package com.tj.demo.utils;import org.apache.commons.lang.StringUtils;import javax.servlet.http.HttpServletRequest;public class TjIpUtils {/**** 获取客户端ip地址* @param request*/public static String getIP(final HttpServletRequest request) throws Exception {if (request == null) {throw (new Exception("getIpAddr method HttpServletRequest Object is null"));}String ipStr = request.getHeader("x-forwarded-for");if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {ipStr = request.getHeader("Proxy-Client-IP");}if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {ipStr = request.getHeader("WL-Proxy-Client-IP");}if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {ipStr = request.getRemoteAddr();}// 多个路由时,取第一个非unknown的ipfinal String[] arr = ipStr.split(",");for (final String str : arr) {if (!"unknown".equalsIgnoreCase(str)) {ipStr = str;break;}}//目的是将localhost访问对应的ip 0:0:0:0:0:0:0:1 转成 127.0.0.1。return ipStr.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipStr;}}
