离线版IP归属地查询

这里简单介绍一下Ip2regionSearcher是什么?

ip2region 是准确率 99.9% 的 IP 地址定位库,0.0x毫秒级查询,提供了 Java、PHP、C、Python、Node.js、Golang、C#、Rust、Lua的查询绑定和 Binary、B树、内存三种查询算法!

Setp1:导入依赖

java 复制代码
<dependency>
    <groupId>net.dreamlu</groupId>
    <artifactId>mica-ip2region</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.11</version>
</dependency>

提示:Ip2regionSearcher的和hutool工具包依赖包,这里导入hutool为了后期利用获取IP工具类

Setp2:编写测试接口

java 复制代码
import cn.hutool.extra.servlet.ServletUtil;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 测试控制器
 * <p>
 *
 * @className: XController
 * @author: BertXie
 * @create: 2023-01-09 11:40
 */

@RestController
@RequiredArgsConstructor
public class XController {

    private final Ip2regionSearcher searchSearcher;

    @GetMapping("/ip")
    public String getHomeLocation(HttpServletRequest request) {
        IpInfo ipInfo = searchSearcher.memorySearch(ServletUtil.getClientIP(request));
        return ipInfo != null ? ipInfo.getCity() : "未知星球";
    }

}

注意:这里 注入了Ip2regionSearcher 对象,并 使用了memorySearch方法 ,会 返回一个IP详情对象 ,还用到了 Hutool工具 的获取IP的方法 ServletUtil.getClientIP

测试效果图

提示:这里我是写的接口,你也可以直接写一个测试类,填入IP即可

相关推荐
半个西瓜.38 分钟前
车联网安全:GPS定位测试.(静态欺骗)
网络·安全·网络安全·车载系统·安全威胁分析
pengyi8710151 小时前
独享IP+动态IP结合核心逻辑,破解稳定与灵活的矛盾
linux·运维·网络
梅羽落6 小时前
MSF基础1
网络·网络协议·tcp/ip
被摘下的星星7 小时前
子网de划分
网络·算法
xiaoshuaishuai87 小时前
C# modbustcp的ack包通信延迟原因
网络·tcp/ip·c#
byoass7 小时前
自动化任务系列之五:PDF批量转换+自动清理——文件格式规范化工作流
网络·人工智能·安全·云计算
LlNingyu8 小时前
简单流量分析,串通Wireshark基本使用
网络·wireshark·流量分析
茉莉玫瑰花茶8 小时前
HTTPS 协议原理
网络协议·https·iphone
byoass8 小时前
自动化任务系列之六:自动整理文件——按时间/关键字分类归档实战
运维·网络·安全·自动化·云计算
skilllite作者8 小时前
OpenClaw 网络数据采集新手入门指南
网络