From c0412257a25bf7d76244d3b6b1c8af7b5060c305 Mon Sep 17 00:00:00 2001 From: zlt Date: Sun, 8 Sep 2019 16:15:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ip=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/central/common/utils/AddrUtil.java | 71 +++++++++++++++++++ .../filter/pre/RequestStatisticsFilter.java | 43 ++--------- 2 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/AddrUtil.java diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/AddrUtil.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/AddrUtil.java new file mode 100644 index 00000000..891c89e4 --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/AddrUtil.java @@ -0,0 +1,71 @@ +package com.central.common.utils; + +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; + +import javax.servlet.http.HttpServletRequest; +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * @author zlt + * @date 2019/9/8 + */ +@Slf4j +public class AddrUtil { + private final static String UNKNOWN_STR = "unknown"; + /** + * 获取客户端IP地址 + */ + public static String getRemoteAddr(HttpServletRequest request) { + String ip = request.getHeader("X-Forwarded-For"); + if (isEmptyIP(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + if (isEmptyIP(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + if (isEmptyIP(ip)) { + ip = request.getHeader("HTTP_CLIENT_IP"); + if (isEmptyIP(ip)) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + if (isEmptyIP(ip)) { + ip = request.getRemoteAddr(); + if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) { + // 根据网卡取本机配置的IP + ip = getLocalAddr(); + } + } + } + } + } + } else if (ip.length() > 15) { + String[] ips = ip.split(","); + for (int index = 0; index < ips.length; index++) { + String strIp = ips[index]; + if (!isEmptyIP(ip)) { + ip = strIp; + break; + } + } + } + return ip; + } + + private static boolean isEmptyIP(String ip) { + if (StrUtil.isEmpty(ip) || UNKNOWN_STR.equalsIgnoreCase(ip)) { + return true; + } + return false; + } + + /** + * 获取本机的IP地址 + */ + public static String getLocalAddr() { + try { + return InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + log.error("InetAddress.getLocalHost()-error", e); + } + return ""; + } +} diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/RequestStatisticsFilter.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/RequestStatisticsFilter.java index bbeabb66..792bbfe6 100644 --- a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/RequestStatisticsFilter.java +++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/RequestStatisticsFilter.java @@ -1,6 +1,6 @@ package com.central.gateway.filter.pre; -import cn.hutool.core.util.StrUtil; +import com.central.common.utils.AddrUtil; import com.central.log.monitor.PointUtil; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; @@ -20,8 +20,6 @@ import javax.servlet.http.HttpServletRequest; @Slf4j @Component public class RequestStatisticsFilter extends ZuulFilter { - private final static String UNKNOWN_STR = "unknown"; - @Override public String filterType() { return FilterConstants.PRE_TYPE; @@ -44,40 +42,11 @@ public class RequestStatisticsFilter extends ZuulFilter { UserAgent userAgent = UserAgent.parseUserAgentString(req.getHeader("User-Agent")); //埋点 - PointUtil.debug("0","request-statistics", - "ip="+this.getIpAddr(req) - +"&browser="+userAgent.getBrowser() - +"&operatingSystem="+userAgent.getOperatingSystem()); + PointUtil.debug("0", "request-statistics", + "ip=" + AddrUtil.getRemoteAddr(req) + + "&browser=" + userAgent.getBrowser() + + "&operatingSystem=" + userAgent.getOperatingSystem()); + return null; } - - /** - * 获取Ip地址 - */ - public String getIpAddr(HttpServletRequest request){ - String ip = request.getHeader("X-Forwarded-For"); - if (isEmptyIP(ip)) { - ip = request.getHeader("Proxy-Client-IP"); - if (isEmptyIP(ip)) { - ip = request.getHeader("WL-Proxy-Client-IP"); - if (isEmptyIP(ip)) { - ip = request.getHeader("HTTP_CLIENT_IP"); - if (isEmptyIP(ip)) { - ip = request.getHeader("HTTP_X_FORWARDED_FOR"); - if (isEmptyIP(ip)) { - ip = request.getRemoteAddr(); - } - } - } - } - } - return ip; - } - - private boolean isEmptyIP(String ip) { - if (StrUtil.isEmpty(ip) || UNKNOWN_STR.equalsIgnoreCase(ip)) { - return true; - } - return false; - } }