优化ip获取
This commit is contained in:
+71
@@ -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 "";
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-37
@@ -1,6 +1,6 @@
|
|||||||
package com.central.gateway.filter.pre;
|
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.central.log.monitor.PointUtil;
|
||||||
import com.netflix.zuul.ZuulFilter;
|
import com.netflix.zuul.ZuulFilter;
|
||||||
import com.netflix.zuul.context.RequestContext;
|
import com.netflix.zuul.context.RequestContext;
|
||||||
@@ -20,8 +20,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class RequestStatisticsFilter extends ZuulFilter {
|
public class RequestStatisticsFilter extends ZuulFilter {
|
||||||
private final static String UNKNOWN_STR = "unknown";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filterType() {
|
public String filterType() {
|
||||||
return FilterConstants.PRE_TYPE;
|
return FilterConstants.PRE_TYPE;
|
||||||
@@ -44,40 +42,11 @@ public class RequestStatisticsFilter extends ZuulFilter {
|
|||||||
UserAgent userAgent = UserAgent.parseUserAgentString(req.getHeader("User-Agent"));
|
UserAgent userAgent = UserAgent.parseUserAgentString(req.getHeader("User-Agent"));
|
||||||
|
|
||||||
//埋点
|
//埋点
|
||||||
PointUtil.debug("0","request-statistics",
|
PointUtil.debug("0", "request-statistics",
|
||||||
"ip="+this.getIpAddr(req)
|
"ip=" + AddrUtil.getRemoteAddr(req)
|
||||||
+"&browser="+userAgent.getBrowser()
|
+ "&browser=" + userAgent.getBrowser()
|
||||||
+"&operatingSystem="+userAgent.getOperatingSystem());
|
+ "&operatingSystem=" + userAgent.getOperatingSystem());
|
||||||
|
|
||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user