如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值

This commit is contained in:
lihy2021 2021-11-07 10:31:44 +00:00 committed by Gitee
parent bbbe83b737
commit 82819cb1e7

View File

@ -20,6 +20,13 @@ public class IpUtils
return "unknown";
}
String ip = request.getHeader("x-forwarded-for");
if (StringUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip))
{
if (ip.indexOf(",") != -1)
{
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("Proxy-Client-IP");