Geo IP lib (ip 地址库)的使用
1. Geo IP lib 地址库说明
如果有以下需求 请参考下面的文档:
如果需要通过IP查询到这个IP所对应的地址就使用 GeoLite2-City.mmdb 数据库文件进行ip的解析 .
2. 下载地址
2.1 Geo IP 官网地址 2.2 库源文件地址: (此链接已经配置了私钥可以直接下载). 2.3 校验文件地址: (此链接已经配置了私钥可以直接下载). 2.4 以上 第二个连接就是IP库的下载地址 已经配置了秘钥为永久地址, 此版本是免费版本的, 所以有一些ip查询不出来, 基本上都是1周左右官方就会更新一次 ip库. 所以这个文件也是需要更新的要不然对应一些比较特殊的ip就会出现查询错误, 或者查询不出来的情况. 所以需要写一个程序每天去获取一下最新的ip库信息并保存到resource目录 重新加载应用.
3. java代码解析数据库文件
3.1. maven 引入jar包
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.8.1</version>
</dependency>
将 下载下来的 GeoLite2-City.mmdb 文件, 拷贝到工程的 resource目录下面.
3.2. 代码使用解析ip
@Data
public class IPAddressDomain {
private String ip
= "";
private String countrySimpleCode
= "";
private String countryEnglishName
= "";
private String subdivisionSimpleCode
= null
;
private String subdivisionEnglisName
= null
;
private String cityEnglishName
= "";
private String citySimpleCode
= "";
private String latitude
= "0";
private String longitude
= "0";
}
public class IpUtils {
public static DatabaseReader reader
= null
;
public static Lock lock
= new ReentrantLock();
public static String
getIpAddr(HttpServletRequest request
){
String ip
= request
.getHeader("X-Real-IP");
if (ip
== null
|| ip
.length() == 0 || "unknown".equalsIgnoreCase(ip
)) {
ip
= request
.getHeader("Proxy-Client-IP");
}
if (ip
== null
|| ip
.length() == 0 || "unknown".equalsIgnoreCase(ip
)) {
ip
= request
.getHeader("WL-Proxy-Client-IP");
}
if (ip
== null
|| ip
.length() == 0 || "unknown".equalsIgnoreCase(ip
)) {
ip
= request
.getHeader("HTTP_CLIENT_IP");
}
if (ip
== null
|| ip
.length() == 0 || "unknown".equalsIgnoreCase(ip
)) {
ip
= request
.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip
== null
|| ip
.length() == 0 || "unknown".equalsIgnoreCase(ip
)) {
ip
= request
.getRemoteAddr();
}
return ip
;
}
private static void init(InputStream inputStream
) throws IOException
{
try {
lock
.lock();
reader
= new DatabaseReader.Builder(inputStream
).build();
} catch (Exception e
) {
e
.printStackTrace();
}finally {
lock
.unlock();
}
}
public static IPAddressDomain
findCityByIp(InputStream inputStream
,InetAddress inetAddress
) throws IOException
, GeoIp2Exception
{
if(reader
== null
){
init(inputStream
);
}
IPAddressDomain ipAddressDomain
= new IPAddressDomain();
CityResponse response
= reader
.city(inetAddress
);
Country country
= response
.getCountry();
ipAddressDomain
.setCountrySimpleCode(country
.getIsoCode());
ipAddressDomain
.setCountryEnglishName(country
.getName());
ipAddressDomain
.setIp(inetAddress
.getHostAddress());
Subdivision subdivision
= response
.getMostSpecificSubdivision();
ipAddressDomain
.setSubdivisionSimpleCode(subdivision
.getIsoCode());
ipAddressDomain
.setSubdivisionEnglisName(subdivision
.getName());
City city
= response
.getCity();
ipAddressDomain
.setCityEnglishName(city
.getName());
ipAddressDomain
.setCitySimpleCode(String
.valueOf(city
.getGeoNameId()));
Postal postal
= response
.getPostal();
ipAddressDomain
.setCitySimpleCode(postal
.getCode());
Location location
= response
.getLocation();
ipAddressDomain
.setLatitude(location
.getLatitude().toString());
ipAddressDomain
.setLongitude(location
.getLongitude().toString());
return ipAddressDomain
;
}
}
public static void main(String
[] args
) throws Exception
{
String ip
= null
;
ip
= "40.79.78.1";
InputStream ins
= getClass().getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");
InetAddress ipAddress
= InetAddress
.getByName(ip
);
IPAddressDomain cityByIp
= IpUtils
.findCityByIp(ins
, ipAddress
);
if (StringUtils
.isNotBlank(cityByIp
.getCountryEnglishName()) && StringUtils
.isBlank(cityByIp
.getCityEnglishName())) {
cityByIp
.setCityEnglishName("unCity");
cityByIp
.setCitySimpleCode("00404");
}
System
.out
.printli(ipAddress
)
}
到此ip库解析结束. 下一篇将解决 自动更新 mmdb文件的问题 其他语言API 参考: 地址
这个IP 工具有些IP 无法查询到 请参考下一篇可以免费查询到 全球IP IP 网段查询https://blog.csdn.net/qq_40158629/article/details/107857759