LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

[转帖]IP 归属地获取,一个依赖轻松搞定

liguoquan
2023年7月20日 17:1 本文热度 356
:IP 归属地获取,一个依赖轻松搞定


IP 归属地获取,一个依赖轻松搞定



为了让网络环境变的更加和谐,现在的主流平台基本都已经添加了IP归属地展示,用于显示内容输出者所属的地域;那我们自己的项目要如何加入IP归属地展示呢?下面通过本地解析+在线获取的方式,轻松搞定归属地获取的需求。

# 依赖:


如果使用本地ip 解析的话,我们将会借助ip2region,该项目维护了一份较为详细的本地ip 地址对应表,如果为了离线环境的使用,需要导入该项目依赖,并指定版本,不同版本的方法可能存在差异。

<!--    ip库--><dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>2.7.0</version></dependency>
官方gitee:https://gitee.com/lionsoul/ip2region


# 本地解析


在使用时需要将 xdb 文件下载到工程文件目录下,使用ip2region即使是完全基于 xdb 文件的查询,单次查询响应时间在十微秒级别,可通过如下两种方式开启内存加速查询:


1.vIndex 索引缓存 :使用固定的 512KiB 的内存空间缓存 vector index 数据,减少一次 IO 磁盘操作,保持平均查询效率稳定在10-20微秒之间。

2.xdb 整个文件缓存:将整个 xdb 文件全部加载到内存,内存占用等同于 xdb 文件大小,无磁盘 IO 操作,保持微秒级别的查询效率。


/** * ip查询 */@Slf4jpublic class IPUtil {
   private static final String UNKNOWN = "unknown";
   protected IPUtil(){ }
   /**     * 获取 IP地址     * 使用 Nginx等反向代理软件, 则不能通过 request.getRemoteAddr()获取 IP地址     * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,     * X-Forwarded-For中第一个非 unknown的有效IP字符串,则为真实IP地址     */    public static String getIpAddr(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.getRemoteAddr();        }        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;    }
   public static  String getAddr(String ip){        String dbPath = "src/main/resources/ip2region/ip2region.xdb";        // 1、从 dbPath 加载整个 xdb 到内存。        byte[] cBuff;        try {            cBuff = Searcher.loadContentfromFile(dbPath);        } catch (Exception e) {            log.info("failed to load content from `%s`: %s\n", dbPath, e);            return null;        }
       // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。        Searcher searcher;        try {            searcher = Searcher.newWithBuffer(cBuff);        } catch (Exception e) {           log.info("failed to create content cached searcher: %s\n", e);            return null;        }        // 3、查询        try {            String region = searcher.searchByStr(ip);            return region;        } catch (Exception e) {            log.info("failed to search(%s): %s\n", ip, e);        }        return null;    }
这里我们将ip 解析封装成一个工具类,包含获取IP和ip 地址解析两个方法,ip 的解析可以在请求中获取。获取到ip后,需要根据ip ,在xdb 中查找对应的IP地址的解析,由于是本地数据库可能存在一定的缺失,部分ip 存在无法解析的情况。

# 在线解析


离线IP库总会出现更新不及时的问题,如果想要获取更加全面的ip 地址信息,可使用在线数据库,这里提供的是 whois.pconline.com  的IP解析,该IP解析在我的使用过程中表现非常流畅,而且只有少数的ip 存在无法解析的情况。

@Slf4jpublic class AddressUtils {    // IP地址查询    public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
   // 未知地址    public static final String UNKNOWN = "XX XX";
   public static String getRealAddressByIP(String ip) {        String address = UNKNOWN;        // 内网不查询        if (IpUtils.internalIp(ip)) {            return "内网IP";        }        if (true) {            try {                String rspStr = sendGet(IP_URL, "ip=" + ip + "&json=true" ,"GBK");                if (StrUtil.isEmpty(rspStr)) {                    log.error("获取地理位置异常 {}" , ip);                    return UNKNOWN;                }                JSONObject obj = JSONObject.parseObject(rspStr);                String region = obj.getString("pro");                String city = obj.getString("city");                return String.format("%s %s" , region, city);            } catch (Exception e) {                log.error("获取地理位置异常 {}" , ip);            }        }        return address;    }
   public static String sendGet(String url, String param, String contentType) {        StringBuilder result = new StringBuilder();        BufferedReader in = null;        try {            String urlNameString = url + "?" + param;            log.info("sendGet - {}" , urlNameString);            URL realUrl = new URL(urlNameString);            URLConnection connection = realUrl.openConnection();            connection.setRequestProperty("accept" , "*/*");            connection.setRequestProperty("connection" , "Keep-Alive");            connection.setRequestProperty("user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");            connection.connect();            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));            String line;            while ((line = in.readLine()) != null) {                result.append(line);            }            log.info("recv - {}" , result);        } catch (ConnectException e) {            log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);        } catch (SocketTimeoutException e) {            log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);        } catch (IOException e) {            log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);        } catch (Exception e) {            log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);        } finally {            try {                if (in != null) {                    in.close();                }            } catch (Exception ex) {                log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);            }        }        return result.toString();    }}


# 案例分析:


那么在开发的什么流程获取ip 地址是比较合适的,这里就要用到我们的拦截器了。拦截进入服务的每个请求,进行前置操作,在进入时就完成请求头的解析,ip 获取以及ip 地址解析,这样在后续流程的全环节,都可以复用ip 地址等信息。


/** * 对ip 进行限制,防止IP大量请求 */@Slf4j@Configurationpublic class IpUrlLimitInterceptor implements HandlerInterceptor{
   @Override    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) {
       //更新全局变量        Constant.IP = IPUtil.getIpAddr(httpServletRequest);        Constant.IP_ADDR = AddressUtils.getRealAddressByIP(Constant.IP);        Constant.URL = httpServletRequest.getRequestURI();        return true;    }
   @Override    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {        //通过本地获取//        获得ip//        String ip = IPUtil.getIpAddr(httpServletRequest);        //解析具体地址//        String addr = IPUtil.getAddr(ip);
       //通过在线库获取//        String ip = IpUtils.getIpAddr(httpServletRequest);//        String ipaddr = AddressUtils.getRealAddressByIP(ipAddr);//        log.info("IP >> {},Address >> {}",ip,ipaddr);    }
   @Override    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
   }}
如果想要执行我们的ip 解析拦截器,需要在spring boot的视图层进行拦截才会触发我们的拦截器。@Configurationpublic class WebConfig implements WebMvcConfigurer {    @Autowired    IpUrlLimitInterceptor ipUrlLimitInterceptor;     //执行ip拦截器    @Override    public void addInterceptors(InterceptorRegistry registry){        registry.addInterceptor(ipUrlLimitInterceptor)                // 拦截所有请求                .addPathPatterns("/**");    }}

通过这样的一套流程下来,我们就能实现对每一个请求进行ip 获取、ip解析,为每个请求带上具体ip地址的小尾巴。

该文章在 2023/7/20 17:01:02 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved