本文分析项目中获取外网IP地址以及调用腾讯地图接口获取IP归属地信息。
一、获取IP
Web项目按如下代码可获取IP地址,需注意获取的是外网地址,本地项目返回为空。
@Autowired
private HttpServletRequest request;
public String getIp(){
String ip = null;
String ipAddresses = request.getHeader("X-Forwarded-For");
if (ipAddresses != null && ipAddresses.length() != 0) {
log.info("ipAddresses:{}", ipAddresses);
ip = ipAddresses.split(",")[0];
log.info("ip:{}", ip);
}
return ip;
}
二、调用腾讯地图接口查询归属地
腾讯SAAS化接口文档请参考:https://fbmt.atlassian.net/wiki/spaces/Tech/pages/155353102/IP,
签名校验使用方法文档参考:https://lbs.qq.com/faq/serverFaq/webServiceKey
三、封装和编码实现
private static final String key = "4COAC-PGS******";
private static final String secret = "TIN3mxcLI*******";
private static final String url = "https://apis.map.qq.com/ws/location/v1/ip";
public static String getIpAddressInfo(String ip){
Map<String, Object> params = new HashMap<>();
params.put("ip",ip);
params.put("key",key);
String param = "/ws/location/v1/ip?ip=" + ip + "&key=" + key + secret;
String sig = getMd5String(param);
params.put("sig",sig);
String s = HttpUtils.doGet(url, params);
JSONObject jsonObject = JSONObject.parseObject(s);
Integer status = jsonObject.getInteger("status");
String message = jsonObject.getString("message");
if (0 != status || !message.equalsIgnoreCase("Success")){
log.error("查询归属地失败");
return "";
}
return jsonObject.toJSONString();
}
public static String getMd5String(String string){
try {
MessageDigest md5 = MessageDigest.getInstance( "MD5");
md5.update((string).getBytes( "UTF-8"));
byte b[] = md5.digest();
int i;
StringBuffer buf = new StringBuffer( "");
for( int offset= 0; offset<b.length; offset++){
i = b[offset];
if(i< 0){
i+= 256;
}
if(i< 16){
buf.append( "0");
}
buf.append(Integer.toHexString(i));
}
return buf.toString();
}catch (Exception e) {
e.printStackTrace();
}
return "";
}
说明:秘钥信息key和secret需配置自己项目上的。加签采用32位小写的MD5。
四、测试
public static void main(String[] args) {
String ip1 = "58.35.49.37"; //上海
String ip2 = "41.74.79.255"; //利比亚
String ip3 = "1.19.255.255"; //韩国
String ip4 = "1.178.223.255"; //香港
String ip5 = "1.5.255.255"; //日本
String ip6 = "36.96.240.0"; //新疆
String ip7 = "2.56.9.255"; //美国
String ip8 = "45.68.3.255"; //玻利维亚
String ip9 = "45.228.66.255"; //乌拉圭
System.out.println(getIpAddressInfo(ip1));
System.out.println(getIpAddressInfo(ip2));
System.out.println(getIpAddressInfo(ip3));
System.out.println(getIpAddressInfo(ip4));
System.out.println(getIpAddressInfo(ip5));
System.out.println(getIpAddressInfo(ip6));
System.out.println(getIpAddressInfo(ip7));
System.out.println(getIpAddressInfo(ip8));
System.out.println(getIpAddressInfo(ip9));
}
运行结果:
{"result":{"ip":"58.35.49.37","ad_info":{"province":"上海市","nation":"中国","city":"上海市","adcode":310115,"district":"浦东新区"},"location":{"lng":121.54409,"lat":31.22114}},"message":"Success","request_id":"bdb772b1d714420faa3ce19ce7268067","status":0}
{"result":{"ip":"41.74.79.255","ad_info":{"province":"","nation":"利比亚","city":"","adcode":-1,"district":""},"location":{"lng":13.1667,"lat":32.8833}},"message":"Success","request_id":"b72a392f88444e5f8f0aa9243416a82e","status":0}
{"result":{"ip":"1.19.255.255","ad_info":{"province":"","nation":"韩国","city":"","adcode":-1,"district":""},"location":{"lng":126.9833,"lat":37.55}},"message":"Success","request_id":"4e5df057fc7c46bca8121b7225966239","status":0}
{"result":{"ip":"1.178.223.255","ad_info":{"province":"香港特别行政区","nation":"中国","city":"香港特别行政区","adcode":810000,"district":""},"location":{"lng":114.16546,"lat":22.27534}},"message":"Success","request_id":"df85bccb42744165a0c33fddb613ac02","status":0}
{"result":{"ip":"1.5.255.255","ad_info":{"province":"","nation":"日本","city":"","adcode":-1,"district":""},"location":{"lng":139.75,"lat":35.6833}},"message":"Success","request_id":"9b4e8aefb9b7497293cd49006ade96d7","status":0}
{"result":{"ip":"36.96.240.0","ad_info":{"province":"","nation":"中国","city":"","adcode":-1,"district":""},"location":{"lng":116.3833,"lat":39.9167}},"message":"Success","request_id":"ce2850f82b8644e0a4410d9502fab1f4","status":0}
{"result":{"ip":"2.56.9.255","ad_info":{"province":"","nation":"美国","city":"","adcode":-1,"district":""},"location":{"lng":-77,"lat":38.8833}},"message":"Success","request_id":"33a6ae1c79b8433a998eafbcde4664ce","status":0}
{"result":{"ip":"45.68.3.255","ad_info":{"province":"","nation":"玻利维亚","city":"","adcode":-1,"district":""},"location":{"lng":-68.15,"lat":-16.5}},"message":"Success","request_id":"a774f9c5817e489a8671072fa72b7075","status":0}
{"result":{"ip":"45.228.66.255","ad_info":{"province":"","nation":"乌拉圭","city":"","adcode":-1,"district":""},"location":{"lng":-56.1667,"lat":-34.85}},"message":"Success","request_id":"1fe8d4cac2dc4b67b1292ca9f98cc06a","status":0}