Java通过GeoLite2-City.mmdb 进行IP信息查询地理定位和经纬度筛选。

引入依赖

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

下载数据文件:https://download.lin2ur.cn/GeoLite2/
在这里插入图片描述

package com.cqcloud.platform.utils;import java.io.File;
import java.net.InetAddress;
import com.maxmind.geoip2.DatabaseReader
import cn.hutool.core.lang.Console;
import lombok.experimental.UtilityClass;/*** @author weimeilayer@gmail.com ✨* @date 💓💕2024年7月4日 🐬🐇 💓💕*/
@UtilityClass
public class GeoLite2Utils {/**** @description: 获得国家* @param reader GeoLite2 数据库* @param ip 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 GeoLite2 数据库* @param ip 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 GeoLite2 数据库* @param ip 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");}/**** @description: 获得经度* @param reader GeoLite2 数据库* @param ip 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 GeoLite2 数据库* @param ip ip地址* @return* @throws Exception*/public static Double getLatitude(DatabaseReader reader, String ip) throws Exception {return reader.city(InetAddress.getByName(ip)).getLocation().getLatitude();}public static void main(String[] args) throws Exception {long startTime = System.currentTimeMillis();Console.log("--------------------------开始时间{}", startTime);// 创建 GeoLite2 数据库//Windows环境切换到自己的文件存放路径即可File file = new File("D:\\GeoLite2-City.mmdb");// 读取数据库内容DatabaseReader reader = new DatabaseReader.Builder(file).build();// 访问IPString ip = "218.70.71.0";String siteAddress = "国家:"+GeoLite2Utils.getCountry(reader, ip) + "\n省份:" + GeoLite2Utils.getProvince(reader, ip) + "\n城市:" + GeoLite2Utils.getCity(reader, ip)+ "\n经度:" + GeoLite2Utils.getLongitude(reader, ip)+ "\n纬度:" + GeoLite2Utils.getLatitude(reader, ip);Console.log(siteAddress);long endTime = System.currentTimeMillis();Console.log("--------------------------结束时间 {} 耗时 {} 毫秒", endTime, endTime - startTime);}
}

运行结果

--------------------------开始时间1720080983354
国家:中国
省份:重庆
城市:重庆市
经度:106.5577
纬度:29.5689
--------------------------结束时间 1720080983436 耗时 82

然后作为判断的 创建 GeoIPChecker

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Subdivision;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Location;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Objects;
import cn.hutool.core.lang.Console;
/*** @author weimeilayer@gmail.com ✨* @date 💓💕2024年7月4日 🐬🐇 💓💕*/
public class GeoIPChecker {private static final String ALLOWED_COUNTRY = "China";private static final String ALLOWED_PROVINCE = "Chongqing";public static void main(String[] args) throws Exception {long startTime = System.currentTimeMillis();Console.log("--------------------------开始时间 " + startTime);// 创建 GeoLite2 数据库File file = new File("D:\\GeoLite2-City.mmdb");// 读取数据库内容DatabaseReader reader = new DatabaseReader.Builder(file).build();// 获取客户端 IPHttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();String ip = JakartaServletUtil.getClientIP(request);// 查询地理位置信息String siteAddress = getGeoLocation(reader, ip);Console.log(siteAddress);// 判断是否在允许范围内if (isAllowed(ip, reader)) {Console.log("允许访问");} else {Console.log("不允许访问");}long endTime = System.currentTimeMillis();Console.log("--------------------------结束时间 " + endTime + " 耗时 " + (endTime - startTime) + " 毫秒");}public static String getGeoLocation(DatabaseReader reader, String ip) throws IOException, GeoIp2Exception {InetAddress ipAddress = InetAddress.getByName(ip);CityResponse response = reader.city(ipAddress);Country country = response.getCountry();Subdivision subdivision = response.getMostSpecificSubdivision();City city = response.getCity();Location location = response.getLocation();return "国家:" + country.getName() +"\n省份:" + subdivision.getName() +"\n城市:" + city.getName() +"\n经度:" + location.getLongitude() +"\n纬度:" + location.getLatitude();}public static boolean isAllowed(String ip, DatabaseReader reader) throws IOException, GeoIp2Exception {InetAddress ipAddress = InetAddress.getByName(ip);CityResponse response = reader.city(ipAddress);// 检查国家和省份是否在允许的范围内String country = response.getCountry().getName();String province = response.getMostSpecificSubdivision().getName();// 使用全局变量进行检查if (ALLOWED_COUNTRY.equals(country) && ALLOWED_PROVINCE.equals(province)) {return true;}return false;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/40932.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【spring MVC的执行流程】

SpringMVC可以说是Servlet的封装&#xff0c;屏蔽了Servlet的很多细节&#xff0c;比如Servlet再获取参数的时候需要不停地getParameter,现在只要在SpringMVC方法定义对应的JavaBean&#xff0c;只要属性和参数名一致&#xff0c;SpringMVC就可以帮我们实现将参数封装到JavaBea…

【Linux】目录和文件的权限意义

现在我们知道了Linux系统内文件的三种身份&#xff08;拥有者、用户组与其他人&#xff09;&#xff0c;知道每种身份都有三种权限&#xff08;rwx&#xff09;&#xff0c;也知道能够使用chown、chgrp、chmod修改这些权限与属性&#xff0c;当然&#xff0c;利用IS-l去查看文件…

wordpress企业网站模板免费下载

大气上档次的wordpress企业模板&#xff0c;可以直接免费下载&#xff0c;连注册都不需要&#xff0c;网盘就可以直接下载&#xff0c;是不是嘎嘎给力呢 演示 https://www.jianzhanpress.com/?p5857 下载 链接: https://pan.baidu.com/s/1et7uMYd6--NJEWx-srMG1Q 提取码:…

【独家揭秘】三星HBM3e内存通过英伟达认证?官方辟谣背后真相何在?

7月初&#xff0c;业界一则震撼消息传来&#xff0c;据韩媒Newdaily报道&#xff0c;三星电子第五代高速存储器HBM3e已成功获得GPU巨头英伟达的质量认证&#xff0c;预示着三星即将步入HBM大规模量产的新阶段。报道援引半导体行业内部消息&#xff0c;称三星近期收到来自英伟达…

Air系列4G模块AT版本如何使用HTTP

HTTP应用的基本流程如下&#xff1a; 1、激活PDP 2、初始化HTTP服务 3、设置HTTP会话参数 4、如果要支持SSL&#xff0c;配置SSL参数 5、如果使用POST命令&#xff0c;输入POST数据 6、发起HTTP请求 7、收到HTTP应答&#xff0c;读取应答数据 8、终止HTTP服务 注意&#xff1a;…

web学习笔记(七十六)

目录 1.wxss和css的区别 2.wxss通过less来编写代码 3.小程序实现下拉刷新的操作 4.定义变量的技巧 5.小程序实现触底加载的操作 1.wxss和css的区别 在wxss中为了做适配我们引入了一个新的计量单位rpx,和vue的适配差不多&#xff0c;将整个屏幕分为750份&#xff0c;1r…

Matplotlib 线条的属性

属性名 描述 agg_filter 一个过滤器函数&#xff0c;它接受一个 (m, n, 3) 浮点数组和一个 dpi 值&#xff0c;并返回一个 (m, n, 3) 数组和距图像左下角的两个偏移量 alpha 标量或无 animated 布尔 antialiased或aa 布尔 clip_box Bbox clip_on 布尔 clip_path …

【Python】数据分析与可视化——文本数据分析

文本数据分析是指对文本数据进行收集、清理、加工和分析的过程&#xff0c;旨在从大量的文本信息中抽取有用的信息和知识 1. 数据收集 使用如Python中的BeautifulSoup库进行网络爬虫&#xff0c;寻找并下载网页上的文本内容 2. 数据清洗 在收集到文本数据后&#xff0c;通常…

嵌入式linux sqlite3读写demo

以下是一个简单的C语言程序&#xff0c;使用SQLite数据库进行读写操作的示例。请确保您已经安装了SQLite3库。 #include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *NotUsed, int argc, char **argv, char **azColNam…

ActiveAnno3D采用主动学习实现领域自适应,实现大规模数据集的快速标注(代码开源)

Abstract 大规模数据集的策划仍然成本高昂且需要大量时间和资源。数据通常需要手动标注&#xff0c;创建高质量数据集的挑战依然存在。在这项工作中&#xff0c;我们使用主动学习填补了多模态3D目标检测研究的空白。我们提出了ActiveAnno3D&#xff0c;这是一种主动学习框架&a…

前端引用vue/element/echarts资源等引用方法Blob下载HTML

前端引用下载vue/element/echarts资源等引用方法 功能需求 需求是在HTML页面中集成Vue.js、Element Plus&#xff08;Element UI的Vue 3版本&#xff09;、ECharts等前端资源&#xff0c;使用Blob下载HTML。 解决方案概述 直接访问线上CDN地址&#xff1a;简单直接&#xff0c…

(c++数组02) 双指针 滑动窗口

977、有序数组的平方 双指针思路&#xff1a;两个指针分别从数组两端开始遍历 class Solution { public:vector<int> sortedSquares(vector<int>& nums) {vector<int> answer(nums.size(), 0);int left 0;int right nums.size() - 1;int k nums.siz…

【踩坑】修复报错Cannot find DGL libdgl_sparse_pytorch_2.2.0.so

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 目录 错误复现 原因分析 解决方法 错误复现 import dgldataset dgl.data.CoraGraphDataset() graph dataset[0] graph.adjacency_matrix() 原因分…

centos通过官网下载安装最新版mysql方案

官网下载步骤&#xff1a; 点击DOCUMENTATION mysql的yum仓库Using the MySQL Yum Repository 向下翻&#xff0c;查看安装命令 点击下载mysql安装包 下载对应的版本 不注册&#xff0c;直接下载社区版 下载好的安装包 安装步骤&#xff1a; 把rpm包导入到服务器…

ConsiStory:无需训练的一致性文本到图像生成技术

随着大规模文本到图像&#xff08;T2I&#xff09;扩散模型的发展&#xff0c;用户可以更自由地通过文本指导图像生成过程。然而&#xff0c;要在不同的提示中保持同一主题的视觉一致性仍然是一个挑战。现有的方法通常需要对模型进行微调或预训练&#xff0c;以教授新词汇来描述…

2.2.1 ROS2案例以及案例分析

1.案例需求 需求1&#xff1a;编写话题通信实现&#xff0c;发布方以某个频率发布一段文本&#xff0c;订阅方订阅消息&#xff0c;并输出在终端。 需求2&#xff1a;编写话题通信实现&#xff0c;发布方以某个频率发布自定义接口消息&#xff0c;订阅方订阅消息&#xff0c;并…

在docker配置Nginx环境配置

应用于商业模式集中&#xff0c;对于各种API的调用&#xff0c;对于我们想要的功能进行暴露&#xff0c;对于不用的进行拦截进行鉴权。用于后面的付费 开发环境 正式上线模式 一、常用命令 停止&#xff1a;docker stop Nginx重启&#xff1a;docker restart Nginx删除服务&a…

Liunx网络配置

文章目录 一、查看网络配置永久修改网卡临时修改网卡 二、查看主机名称 hostname三、查看路由表条目 route四、查看网络连接情况netstat五、获取socket统计信息ss六、查看当前系统中打开的文件和进程的工具lsof七、测试网络连通性ping八、跟踪数据包 traceroute九、域名解析 ns…

emacs 重新加载磁盘上的文件

------------------------------------------------------------ author: hjjdebug date: 2024年 07月 04日 星期四 14:05:25 CST descriptor: emacs 重新加载磁盘上的文件 ------------------------------------------------------------ 当我们修改了磁盘上文件&#xff0c;…

【vmbox centos7 网络配置】【centos7 glances 安装】【centos7 安装MySQL5.7】

文章目录 vmbox centos7 网络配置centos7 修改镜像地址centos7 安装 glancesCentOS 7 上安装 MySQL 5.7 并进行基本的安全配置使用 firewalld 开放 3306 端口 可以远程连接mysql vmbox centos7 网络配置 目前 能组建集群 虚拟机网络互通&#xff0c;虚拟机能访问外网 创建一个…