离线版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即可

相关推荐
白帽黑客沐瑶3 天前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
树码小子3 天前
Java网络编程:(socket API编程:TCP协议的 socket API -- 回显程序的服务器端程序的编写)
java·网络·tcp/ip
绿箭柠檬茶3 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
FPGA_Linuxer3 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
real 13 天前
传输层协议UDP
网络·网络协议·udp
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
喵手3 天前
玩转Java网络编程:基于Socket的服务器和客户端开发!
java·服务器·网络
徐子元竟然被占了!!3 天前
实验-基本ACL
网络
ftpeak3 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
LabVIEW开发3 天前
LabVIEW气体污染无线监测
网络·labview·labview知识·labview功能·labview程序