From 1bfb60a06ad5c9db6e4d1e4d0f1075a5fc5bbff1 Mon Sep 17 00:00:00 2001 From: toesbieya <1647775459@qq.com> Date: Sun, 16 Aug 2020 11:52:23 +0800 Subject: [PATCH] =?UTF-8?q?IpUtil=E4=BF=AE=E6=94=B9=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E4=BA=86=E6=B7=98=E5=AE=9D=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toesbieya/jxc/web/common/util/IpUtil.java | 21 +++------------ .../java/cn/toesbieya/jxc/util/IpUtil.java | 26 +++++-------------- 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/java/cloud/web/web-common/src/main/java/cn/toesbieya/jxc/web/common/util/IpUtil.java b/java/cloud/web/web-common/src/main/java/cn/toesbieya/jxc/web/common/util/IpUtil.java index c3bd496..125d7b0 100644 --- a/java/cloud/web/web-common/src/main/java/cn/toesbieya/jxc/web/common/util/IpUtil.java +++ b/java/cloud/web/web-common/src/main/java/cn/toesbieya/jxc/web/common/util/IpUtil.java @@ -4,6 +4,7 @@ import cn.toesbieya.jxc.common.util.RedisUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest; @@ -18,7 +19,6 @@ public class IpUtil { handlerList.add(new PconlineApi()); handlerList.add(new IphelpApi()); handlerList.add(new IpipApi()); - handlerList.add(new TaobaoApi()); } private static final String REDIS_IP_CACHE_KEY = "ipCache"; @@ -64,22 +64,7 @@ public class IpUtil { } private static boolean invalidIp(String ip) { - return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); - } - - //淘宝 部分ip无法查询 - private static class TaobaoApi implements Function { - @Override - public String apply(String ip) { - String url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; - String response = HttpUtil.get(url); - JSONObject result = JSON.parseObject(response); - if (!result.getInteger("code").equals(0) || result.get("data") == null) { - return null; - } - result = result.getJSONObject("data"); - return handleAddress(result.getString("region"), result.getString("city")); - } + return StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip); } private static class IphelpApi implements Function { @@ -92,7 +77,7 @@ public class IpUtil { return null; } JSONArray array = result.getJSONArray("data"); - if (array.size() == 0) return null; + if (CollectionUtils.isEmpty(array)) return null; result = array.getJSONObject(0); return handleAddress(result.getString("province"), result.getString("city")); } diff --git a/java/local/src/main/java/cn/toesbieya/jxc/util/IpUtil.java b/java/local/src/main/java/cn/toesbieya/jxc/util/IpUtil.java index 5436e83..5fb7e69 100644 --- a/java/local/src/main/java/cn/toesbieya/jxc/util/IpUtil.java +++ b/java/local/src/main/java/cn/toesbieya/jxc/util/IpUtil.java @@ -3,6 +3,7 @@ package cn.toesbieya.jxc.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; @@ -20,7 +21,6 @@ public class IpUtil { handlerList.add(new PconlineApi()); handlerList.add(new IphelpApi()); handlerList.add(new IpipApi()); - handlerList.add(new TaobaoApi()); } private static final String REDIS_IP_CACHE_KEY = "ipCache"; @@ -53,7 +53,8 @@ public class IpUtil { for (Function handler : handlerList) { try { cache = handler.apply(ip); - } catch (Exception e) { + } + catch (Exception e) { continue; } if (!StringUtils.isEmpty(cache)) { @@ -65,22 +66,7 @@ public class IpUtil { } private static boolean invalidIp(String ip) { - return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); - } - - //淘宝 部分ip无法查询 - private static class TaobaoApi implements Function { - @Override - public String apply(String ip) { - String url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; - String response = restTemplate.getForObject(url, String.class); - JSONObject result = JSON.parseObject(response); - if (!result.getInteger("code").equals(0) || result.get("data") == null) { - return null; - } - result = result.getJSONObject("data"); - return handleAddress(result.getString("region"), result.getString("city")); - } + return StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip); } private static class IphelpApi implements Function { @@ -93,7 +79,7 @@ public class IpUtil { return null; } JSONArray array = result.getJSONArray("data"); - if (array.size() == 0) return null; + if (CollectionUtils.isEmpty(array)) return null; result = array.getJSONObject(0); return handleAddress(result.getString("province"), result.getString("city")); } @@ -106,7 +92,7 @@ public class IpUtil { String url = "https://freeapi.ipip.net/" + ip; String response = restTemplate.getForObject(url, String.class); JSONArray result = JSON.parseArray(response); - if (result.size() != 5) return null; + if (result == null || result.size() != 5) return null; return handleAddress(result.getString(1), result.getString(2)); } }