现在,各大平台都新增了评论区显示发言者ip归属地的功能,例如哔哩哔哩,微博,知乎等等。本文主要讲解Java中是如何获取ip属地的。
主要分为以下两步
首先需要写一个 IP 获取的工具,因为每一次用户的 Request 请求,都会携带上请求的 IP 地址放到请求头中。
/**
* 获取http请求ip
* @param request
* @return ipAddress
*/
public String getIpAddr(HttpServletRequest request) {
String ipAddress ;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
assert inet != null;
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress="";
}
// ipAddress = this.getRequest().getRemoteAddr();
return ipAddress;
}
对这里出现的几个名词解释一下:
想要拿到 ip 的归属地是哪里,我们需要用到 Ip2region 离线 IP 地址定位库。可以自行去下载: https://github.com/lionsoul2014/ip2region
ip2region - 是一个准确率 99.9% 的离线 IP 地址定位库,0.0x 毫秒级查询,ip2region.db 数据库只有数MB,提供了 java,php,c,python,nodejs,golang,c# 等查询绑定和Binary,B树,内存三种查询算法。
全部的查询客户端单次查询都在 0.x 毫秒级别,内置了三种查询算法
1、引入Maven依赖
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
2、下载仓库中的ip2region.db 文件,放到工程resources目录下:
3、编写方法加载ip2region文件,对用户ip地址进行转换
public String getCityInfo(String ip) {
try {
String dbPath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("ip2region.db")).getPath();
DbSearcher searcher = new DbSearcher(new DbConfig(), dbPath);
//查询算法
//B-tree, B树搜索(更快)
int algorithm = DbSearcher.BTREE_ALGORITHM;
//define the method
Method method;
method = searcher.getClass().getMethod("btreeSearch", String.class);
DataBlock dataBlock;
if (!Util.isIpAddress(ip)) {
LOGGER.error("Error: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
String ipInfo = dataBlock.getRegion();
if (ObjectUtil.isNotEmpty(ipInfo)) {
ipInfo = ipInfo.replace("|0", "");
ipInfo = ipInfo.replace("0|", "");
}
return ipInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
4、我们还需要对这个方法进行封装,得到获取 IP 属地的信息。
/**
* 获取IP属地
* @param ip
*/
public String getIpPossession(String ip) {
String cityInfo = getCityInfo(ip);
if (ObjectUtil.isNotEmpty(cityInfo)) {
cityInfo = cityInfo.replace("|", " ");
String[] cityList = cityInfo.split(" ");
if (cityList.length > 0) {
// 国内的显示到具体的省
if ("中国".equals(cityList[0])) {
if (cityList.length > 1) {
return cityList[0]+"-"+cityList[1]+"-"+cityList[2];
}
}
// 国外显示到国家
return cityList[0];
}
}
return "未知";
}
由于我们线上部署是通过nginx转发代理的,我们要对nginx修改一下配置使其得到客户端真实的 IP
location / {
client_max_body_size 200M;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_pass http://127.0.0.1:8088;
#得到客户端真实的 IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}