From 06df8d78bf04e3fbac5f72dce6696f2c07a4362a Mon Sep 17 00:00:00 2001 From: toesbieya <1647775459@qq.com> Date: Thu, 5 Mar 2020 17:58:57 +0800 Subject: [PATCH] =?UTF-8?q?ip=E5=9C=B0=E5=9D=80=E6=9F=A5=E8=AF=A2=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/toesbieya/my/service/RecService.java | 2 +- .../java/com/toesbieya/my/utils/HttpUtil.java | 52 -------- .../java/com/toesbieya/my/utils/IpUtil.java | 120 +++++++++++++++--- 3 files changed, 100 insertions(+), 74 deletions(-) delete mode 100644 java/src/main/java/com/toesbieya/my/utils/HttpUtil.java diff --git a/java/src/main/java/com/toesbieya/my/service/RecService.java b/java/src/main/java/com/toesbieya/my/service/RecService.java index 71f6128..89ef3f2 100644 --- a/java/src/main/java/com/toesbieya/my/service/RecService.java +++ b/java/src/main/java/com/toesbieya/my/service/RecService.java @@ -74,7 +74,7 @@ public class RecService { .type(historyEnum.getCode()) .time(System.currentTimeMillis()) .build(); - history.setAddress(IpUtil.getIpAddr(ip)); + history.setAddress(IpUtil.getIpAddress(ip)); loginHistoryMapper.insert(history); } diff --git a/java/src/main/java/com/toesbieya/my/utils/HttpUtil.java b/java/src/main/java/com/toesbieya/my/utils/HttpUtil.java deleted file mode 100644 index aa073d9..0000000 --- a/java/src/main/java/com/toesbieya/my/utils/HttpUtil.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.toesbieya.my.utils; - -import com.alibaba.fastjson.JSON; -import com.google.common.base.Joiner; -import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; -import org.springframework.web.client.RestClientException; -import org.springframework.web.client.RestTemplate; - -import java.util.Map; - -@Slf4j -public class HttpUtil { - private static final RestTemplate restTemplate = new RestTemplate(); - - public static String get(String url) { - return httpGet(url, String.class); - } - - public static T get(String url, Class t) { - return httpGet(url, t); - } - - public static T get(String url, Class t, Map map) { - return httpGet(url + toUrlParams(map), t); - } - - @SuppressWarnings("unchecked") - private static T httpGet(String url, Class t) { - String response; - try { - response = restTemplate.getForObject(url, String.class); - } catch (RestClientException e) { - log.error("http请求{}出错:{}", url, e); - return null; - } - if (StringUtils.isEmpty(response)) { - return null; - } - if (t == String.class) { - return (T) response; - } - return JSON.parseObject(response, t); - } - - private static String toUrlParams(Map source) { - if (source.size() <= 0) { - return ""; - } - return "?" + Joiner.on("&").useForNull("").withKeyValueSeparator("=").join(source); - } -} diff --git a/java/src/main/java/com/toesbieya/my/utils/IpUtil.java b/java/src/main/java/com/toesbieya/my/utils/IpUtil.java index 0a0acea..68bddd8 100644 --- a/java/src/main/java/com/toesbieya/my/utils/IpUtil.java +++ b/java/src/main/java/com/toesbieya/my/utils/IpUtil.java @@ -1,12 +1,26 @@ package com.toesbieya.my.utils; -import com.toesbieya.my.model.vo.IpResultVo; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import org.springframework.util.StringUtils; +import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; public class IpUtil { - private static final String API = "http://ip.taobao.com/service/getIpInfo.php"; + private static final RestTemplate restTemplate = new RestTemplate(); + + private static List handlerList = new ArrayList<>(); + + static { + 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"; @@ -27,34 +41,98 @@ public class IpUtil { return ip; } - public static String getIpAddr(String ip) { + public static String getIpAddress(String ip) { if (StringUtils.isEmpty(ip) || ip.equals("127.0.0.1")) { return null; } - String redisCache = (String) RedisUtil.hget(REDIS_IP_CACHE_KEY, ip); - if (!StringUtils.isEmpty(redisCache)) { - return redisCache; + String cache = (String) RedisUtil.hget(REDIS_IP_CACHE_KEY, ip); + if (!StringUtils.isEmpty(cache)) { + return cache; } - IpResultVo vo = HttpUtil.get(API + "?ip=" + ip, IpResultVo.class); - if (vo == null || vo.getCode() != 0 || vo.getData() == null) { - return null; + if (handlerList.size() == 0) return null; + for (IpAddressRequestHandler handler : handlerList) { + try { + cache = handler.getIpAddress(ip); + } catch (Exception e) { + continue; + } + if (!StringUtils.isEmpty(cache)) { + RedisUtil.hset(REDIS_IP_CACHE_KEY, ip, cache); + return cache; + } + } + return null; + } + + private static boolean invalidIp(String ip) { + return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); + } + + private interface IpAddressRequestHandler { + String getIpAddress(String ip); + } + + //淘宝 部分ip无法查询 + private static class TaobaoApi implements IpAddressRequestHandler { + @Override + public String getIpAddress(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")); } - String region = vo.getData().getString("region"); - String city = vo.getData().getString("city"); - if (StringUtils.isEmpty(region) || StringUtils.isEmpty(city)) { - redisCache = null; + } + + private static class IphelpApi implements IpAddressRequestHandler { + @Override + public String getIpAddress(String ip) { + String url = "https://ip.help.bj.cn/?ip=" + ip; + String response = restTemplate.getForObject(url, String.class); + JSONObject result = JSON.parseObject(response); + if (!result.getString("status").equals("200") || result.get("data") == null) { + return null; + } + JSONArray array = result.getJSONArray("data"); + if (array.size() == 0) return null; + result = array.getJSONObject(0); + return handleAddress(result.getString("province"), result.getString("city")); } - else if (region.equals(city)) { - redisCache = region; + } + + //https://www.ipip.net/support/api.html 每天限1000次 + private static class IpipApi implements IpAddressRequestHandler { + @Override + public String getIpAddress(String ip) { + 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; + return handleAddress(result.getString(1), result.getString(2)); } - else { - redisCache = region + city; + } + + //这个不错 + private static class PconlineApi implements IpAddressRequestHandler { + @Override + public String getIpAddress(String ip) { + String url = "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip; + String response = restTemplate.getForObject(url, String.class); + JSONObject result = JSON.parseObject(response); + return handleAddress(result.getString("pro"), result.getString("city")); } - RedisUtil.hset(REDIS_IP_CACHE_KEY, ip, redisCache); - return redisCache; } - private static boolean invalidIp(String ip) { - return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); + private static String handleAddress(String province, String city) { + if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city)) { + return null; + } + else if (province.equals(city)) { + return province; + } + return province + city; } }