Commit 3bf651e3 authored by 张永's avatar 张永

修改取IP方法

parent 8c16df69
......@@ -108,6 +108,11 @@
<artifactId>commons-net</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
......@@ -151,7 +156,7 @@
</dependency>
<!-- 简体转繁体end -->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html-->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
......
......@@ -13,29 +13,78 @@ import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import com.egolm.common.StringUtil;
import com.egolm.common.exception.PluginException;
public class ServletUtil {
public static String remoteIp(HttpServletRequest request) {
String ip = request.getHeader("X-Real_IP");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("X-Forwarded-For");
if (request == null)
{
return "unknown";
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getRemoteAddr();
}
String[] ips = ip.trim().split(", ");
return ips[ips.length - 1].trim();
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
}
/**
* 从多级反向代理中获得第一个非unknown IP地址
*
* @param ip 获得的IP地址
* @return 第一个非unknown IP地址
*/
public static String getMultistageReverseProxyIp(String ip)
{
// 多级反向代理检测
if (ip != null && ip.indexOf(",") > 0)
{
final String[] ips = ip.trim().split(",");
for (String subIp : ips)
{
if (false == isUnknown(subIp))
{
ip = subIp;
break;
}
}
}
return StringUtils.substring(ip, 0, 255);
}
/**
* 检测给定字符串是否为未知,多用于检测HTTP请求相关
*
* @param checkString 被检测的字符串
* @return 是否未知
*/
public static boolean isUnknown(String checkString)
{
return StringUtil.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
}
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
......
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