引入依赖
<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;}
}