Commit 062c6d77 authored by 向怀芳's avatar 向怀芳 🎱

1. 代理IP处理03

parent 767a02be
......@@ -41,6 +41,7 @@ import java.util.*;
public class WebUtils extends org.springframework.web.util.WebUtils {
private final String BASIC_ = "Basic ";
private final String UNKNOWN = "unknown";
private final String LOCAL127 = "127.0.0.1";
/**
* 判断是否ajax请求
......@@ -196,33 +197,32 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
public String getIP(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String ip = headers.getFirst("x-forwarded-for");
log.error("headers = {}", JSONUtil.toJsonStr(headers));
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isNotBlank(ip) && !UNKNOWN.equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
if (ip.indexOf(",") != -1) {
if ("127.0.0.1".equals(ip.split(",")[0])) {
if (LOCAL127.equals(ip.split(",")[0])) {
ip = ip.split(",")[1];
} else {
ip = ip.split(",")[0];
}
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = headers.getFirst("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = headers.getFirst("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = headers.getFirst("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddress().getAddress().getHostAddress();
}
return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
......
......@@ -65,7 +65,7 @@ public class RequestGlobalFilter implements GlobalFilter, Ordered {
// IP白名单
String ip = WebUtils.getIP(request);
log.error("RemoteAddress = {}, ip = {}", request.getRemoteAddress(), ip);
// log.error("RemoteAddress = {}, ip = {}", request.getRemoteAddress(), ip);
try {
if (whitIPConfig.getLimitFlag() && !IPStrUtil.matches(ip, whitIPConfig.getWhites())) {
ServerHttpResponse response = exchange.getResponse();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment