本文主要是介绍IP地址解析省份区域信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景
最近工作需要根据IP地址,解析通话所在省份。因此,在网上找了相关方案,作为存档记录下来。
在线接口
不做阐述。因为,一是网上可以很轻松的找到,没有必要多说。二是开发在内网中,多数不会让连接外网。三是多数花钱的方案,公司审批麻烦。
离线方案
调研了开源的ip2region,但是其不支持IPV6,因此使用mica-ip2region版。mica-ip2region,已内置了IP数据源,因此不需要自己单独添加IP数据源。如果,公司有自己的IP库,需要添加到数据源,需要研究ip2region如何生成IP数据源。
- 依赖
<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><mica.ip2region.version>2.7.18.5</mica.ip2region.version><spring.version>2.7.13</spring.version></properties><dependencies><dependency><groupId>net.dreamlu</groupId><artifactId>mica-ip2region</artifactId><version>${mica.ip2region.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>${spring.version}</version></dependency></dependencies>
- 测试案例
package com.dayue;import net.dreamlu.mica.ip2region.config.Ip2regionConfiguration;
import net.dreamlu.mica.ip2region.config.Ip2regionProperties;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.impl.Ip2regionSearcherImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Demo {private static Ip2regionSearcher searcher;public static void main(String[] args) throws Exception {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();Ip2regionConfiguration configuration = new Ip2regionConfiguration();searcher = configuration.ip2regionSearcher(context, new Ip2regionProperties());((Ip2regionSearcherImpl) searcher).afterPropertiesSet();System.out.println(searcher.memorySearch("220.248.12.158"));System.out.println(searcher.memorySearch("222.240.36.135"));System.out.println(searcher.memorySearch("172.30.13.97"));System.out.println(searcher.memorySearch("223.26.64.0"));System.out.println(searcher.memorySearch("223.26.128.0"));System.out.println(searcher.memorySearch("223.26.67.0"));System.out.println(searcher.memorySearch("223.29.220.0"));System.out.println(searcher.memorySearch("82.120.124.0"));System.out.println(searcher.memorySearch("::ffff:1111:2222"));System.out.println(searcher.memorySearch("2001:db8::ffff:1111:2222"));System.out.println(searcher.memorySearch("::1"));System.out.println(searcher.memorySearch("2406:840:20::1"));System.out.println(searcher.memorySearch("2c0f:feb0:a::"));System.out.println(searcher.memorySearch("240e:109:8047::"));System.out.println(searcher.memorySearch("1111:1111:1111::1111"));}}
这篇关于IP地址解析省份区域信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!