IpUtil修改,移除了淘宝接口

master
toesbieya 6 years ago
parent 70e2af5f68
commit 1bfb60a06a
  1. 21
      java/cloud/web/web-common/src/main/java/cn/toesbieya/jxc/web/common/util/IpUtil.java
  2. 26
      java/local/src/main/java/cn/toesbieya/jxc/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<String, String> {
@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<String, String> {
@ -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"));
}

@ -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<String, String> 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<String, String> {
@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<String, String> {
@ -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));
}
}

Loading…
Cancel
Save