IP查询地理位置 离线版 非第三方接口 根据IP查地理位置 springboot + MaxMind GeoIP2

相信大家在开发中一定有遇到过查询IP地理位置的需求吧

我看网上很多都是去调用第三方的接口

众所周知调用第三方的接口是完全没有保障性的,你不可能确保别人的服务器不出问题。

所以最好还是要我们自己集成。

我们采用的是MaxMind GeoIP2  https://www.maxmind.com/en/home 这是他的官网

 

一、第一步 下载离线资源包

下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/#Downloads

下载之后解压会得到一个GeoLite2-City.mmdb文件,把这个文件放到你的磁盘就好。

二、第二步 引入依赖


    com.maxmind.geoip2
    geoip2
    2.12.0

 三、第三步 工具类

import com.maxmind.geoip2.DatabaseReader;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

public class IpUtil {

    /**
     * 获取IP地址
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        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();
        }

        if(ip == null){
            ip = " ";
        }
        return ip;
    }

    /**
     *
     * @description: 获得国家
     * @param reader
     * @param ip
     * @return
     * @throws Exception
     */
    public static String getCountry(DatabaseReader reader, String ip) throws Exception {
        return reader.city(InetAddress.getByName(ip)).getCountry().getNames().get("zh-CN");
    }

    /**
     *
     * @description: 获得省份
     * @param reader
     * @param ip
     * @return
     * @throws Exception
     */
    public static String getProvince(DatabaseReader reader, String ip) throws Exception {
        return reader.city(InetAddress.getByName(ip)).getMostSpecificSubdivision().getNames().get("zh-CN");
    }

    /**
     *
     * @description: 获得城市
     * @param reader
     * @param ip
     * @return
     * @throws Exception
     */
    public static String getCity(DatabaseReader reader, String ip) throws Exception {
        return reader.city(InetAddress.getByName(ip)).getCity().getNames().get("zh-CN");
    }

    /**
     * 获得详细地址
     * @param reader
     * @param ip
     * @return
     * @throws Exception
     */
    public static String getAddress(DatabaseReader reader,String ip) throws Exception{
        String country = reader.city(InetAddress.getByName(ip)).getCountry().getNames().get("zh-CN");
        String province = reader.city(InetAddress.getByName(ip)).getMostSpecificSubdivision().getNames().get("zh-CN");
        String city = reader.city(InetAddress.getByName(ip)).getCity().getNames().get("zh-CN");
        String f = "-";
        return country+f+province+f+city;
    }

    /**
     *
     * @description: 获得经度
     * @param reader
     * @param ip
     * @return
     * @throws Exception
     */
    public static Double getLongitude(DatabaseReader reader, String ip) throws Exception {
        return reader.city(InetAddress.getByName(ip)).getLocation().getLongitude();
    }

    /**
     *
     * @description: 获得纬度
     * @param reader
     * @param ip
     * @return
     * @throws Exception
     */
    public static Double getLatitude(DatabaseReader reader, String ip) throws Exception {
        return reader.city(InetAddress.getByName(ip)).getLocation().getLatitude();
    }
}

 四、第四步 配置类

import com.maxmind.geoip2.DatabaseReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;

@Configuration
public class IpDatabaseConfig {

    @Value("${ip-database-path}")
    private String path;

    @Bean(destroyMethod="close")
    public DatabaseReader databaseReader(){
        try {
            return new DatabaseReader.Builder(new File(path)).build();
        } catch (IOException e) {
            System.out.println("IPDatabase error:"+e.getMessage());
            return null;
        }
    }
}
@Value("${express.ip-database-path}")
private String path; 

这里你当然你可以不采用在配置文件定义,你可以直接赋值。例如:

private String path = “/usr/local/GeoLite2-City.mmdb"; 

 经过以上这几步,就可以开始使用了。

结果

    @Autowired
    DatabaseReader databaseReader;

    @Test
    public void contextLoads() throws Exception {
        String ip = "113.118.96.221";
        String address = IpUtil.getAddress(databaseReader,ip);
        System.out.println("您的IP位置是:"+address);
    }

 成功调用!

你可能感兴趣的:(学习)