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

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

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

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

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

我们采用的是MaxMind GeoIP2  IP Geolocation and Online Fraud Prevention | MaxMind 这是他的官网

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

下载地址:GeoLite2 Free Geolocation Data | MaxMind Developer Portal

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

二、第二步 引入依赖

<dependency>
    <groupId>com.maxmind.geoip2</groupId>
    <artifactId>geoip2</artifactId>
    <version>2.12.0</version>
</dependency>

 三、第三步 工具类

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);
    }

 成功调用!

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值