ip地址查询修改

master
toesbieya 6 years ago
parent e1a3d536c3
commit 06df8d78bf
  1. 2
      java/src/main/java/com/toesbieya/my/service/RecService.java
  2. 52
      java/src/main/java/com/toesbieya/my/utils/HttpUtil.java
  3. 120
      java/src/main/java/com/toesbieya/my/utils/IpUtil.java

@ -74,7 +74,7 @@ public class RecService {
.type(historyEnum.getCode()) .type(historyEnum.getCode())
.time(System.currentTimeMillis()) .time(System.currentTimeMillis())
.build(); .build();
history.setAddress(IpUtil.getIpAddr(ip)); history.setAddress(IpUtil.getIpAddress(ip));
loginHistoryMapper.insert(history); loginHistoryMapper.insert(history);
} }

@ -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> T get(String url, Class<T> t) {
return httpGet(url, t);
}
public static <T> T get(String url, Class<T> t, Map<String, Object> map) {
return httpGet(url + toUrlParams(map), t);
}
@SuppressWarnings("unchecked")
private static <T> T httpGet(String url, Class<T> 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<String, Object> source) {
if (source.size() <= 0) {
return "";
}
return "?" + Joiner.on("&").useForNull("").withKeyValueSeparator("=").join(source);
}
}

@ -1,12 +1,26 @@
package com.toesbieya.my.utils; 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.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
public class IpUtil { 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<IpAddressRequestHandler> 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"; private static final String REDIS_IP_CACHE_KEY = "ipCache";
@ -27,34 +41,98 @@ public class IpUtil {
return ip; return ip;
} }
public static String getIpAddr(String ip) { public static String getIpAddress(String ip) {
if (StringUtils.isEmpty(ip) || ip.equals("127.0.0.1")) { if (StringUtils.isEmpty(ip) || ip.equals("127.0.0.1")) {
return null; return null;
} }
String redisCache = (String) RedisUtil.hget(REDIS_IP_CACHE_KEY, ip); String cache = (String) RedisUtil.hget(REDIS_IP_CACHE_KEY, ip);
if (!StringUtils.isEmpty(redisCache)) { if (!StringUtils.isEmpty(cache)) {
return redisCache; return cache;
} }
IpResultVo vo = HttpUtil.get(API + "?ip=" + ip, IpResultVo.class); if (handlerList.size() == 0) return null;
if (vo == null || vo.getCode() != 0 || vo.getData() == null) { for (IpAddressRequestHandler handler : handlerList) {
return null; 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)) { private static class IphelpApi implements IpAddressRequestHandler {
redisCache = null; @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) { private static String handleAddress(String province, String city) {
return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city)) {
return null;
}
else if (province.equals(city)) {
return province;
}
return province + city;
} }
} }

Loading…
Cancel
Save