springboot+geoIp2,获得接口调用者的IP,并得到该IP的详细地址,洲,国家。

背景:
获得接口访问者的IP,并解析该IP所在的大洲和国家。
需要下载geoIp的库,在官网注册下载或者CSDN也有资源。

直接上代码:
pom:
下面是geoip2的pom。

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

yaml文件中geoip库的配置地址:

maxmind:
  geoip:
    geolite2-city-mmdb: classpath:location/data.mmdb

config类:

@Configuration
@ConditionalOnProperty(prefix = MaxmindGeoIPProperties.PROPERTY_PREFIX, value = { "enabled" }, matchIfMissing = true)
@EnableConfigurationProperties(MaxmindGeoIPProperties.class)
public class MaxmindGeoIPAutoconfiguration {

	@Autowired
	private MaxmindGeoIPProperties properties;
	
	@Bean
	public DatabaseReader geoIpDatabaseReader() throws Exception {
		return new DatabaseReader.Builder(properties.getGeolite2CityMmdb().getInputStream()).build();
	}

	@Bean
	public GeoIPService geoIpService() throws Exception {
		return new GeoIPService(geoIpDatabaseReader());
	}
	
}
@ConfigurationProperties(prefix = MaxmindGeoIPProperties.PROPERTY_PREFIX)
@Data
public class MaxmindGeoIPProperties {
	
	public static final String PROPERTY_PREFIX = "maxmind.geoip";
	
	private Boolean enabled = true;
	
	private Resource geolite2CityMmdb;


}

service:


@Component
public class GeoIPService {
	
	private DatabaseReader reader;

	public GeoIPService(DatabaseReader geoIpDatabaseReader) {
		this.reader = geoIpDatabaseReader;
	}

	public CityResponse countryResponse(InetAddress inetAddress) {
		try {
			return reader.city(inetAddress);
		} catch (IOException e) {
		} catch (GeoIp2Exception e) {
		}
		
		return null;
	}
	
	public CityResponse countryResponse(String ipAddress) {
		InetAddress inetAddress;
		try {
			inetAddress = InetAddress.getByName(ipAddress);
			return countryResponse(inetAddress);
		} catch (UnknownHostException e) {
		}
		return null;
	}
	

调用者:

    @Autowired
    private GeoIPService geoIPService;

    @Bean
    public String test(){
        String ip = "******";
        System.out.println(geoIPService.countryResponse(ip).getCountry().getNames().get("zh-CN"));
        return "666";
    }

输出的是国家的中文名。

OK,代码到这就结束了,我们来看看geoip能分析出什么给我们,结果返回在geoIPService.countryResponse(ip)这个代码中,将结果转为json后是如下内容:

[{
	"city": {},
	"continent": {
		"code": "NA",
		"geoname_id": 6255149,
		"names": {
			"de": "Nordamerika",
			"ru": "Северная Америка",
			"pt-BR": "América do Norte",
			"ja": "北アメリカ",
			"en": "North America",
			"fr": "Amérique du Nord",
			"zh-CN": "北美洲",
			"es": "Norteamérica"
		}
	},
	"country": {
		"geoname_id": 6252001,
		"iso_code": "US",
		"names": {
			"de": "USA",
			"ru": "США",
			"pt-BR": "Estados Unidos",
			"ja": "アメリカ*",
			"en": "United States",
			"fr": "États-Unis",
			"zh-CN": "美国",
			"es": "Estados Unidos"
		}
	},
	"location": {
		"accuracy_radius": 1000,
		"latitude": ****,
		"longitude": ****,
		"time_zone": "America/Chicago"
	},
	"maxmind": {},
	"postal": {},
	"registered_country": {
		"geoname_id": 6252001,
		"iso_code": "US",
		"names": {
			"de": "USA",
			"ru": "США",
			"pt-BR": "Estados Unidos",
			"ja": "アメリカ*",
			"en": "United States",
			"fr": "États-Unis",
			"zh-CN": "美国",
			"es": "Estados Unidos"
		}
	},
	"represented_country": {},
	"traits": {
		"ip_address": "*****",
		"is_anonymous_proxy": false,
		"is_legitimate_proxy": false,
		"is_satellite_provider": false
	}
}]

可以看出会返回城市信息(博主这个例子没有),国家信息,大洲,如果需要对应的 只需要调用对应的get方法就行,例如返回大洲的中文名:getContinent().getNames().get(“zh-CN”);

遇到的坑:
1.读取resource下的文件不要用new File的方式,在linux使用jar包运行会报filenotFound错误。
2.下下来的mmdb文件要改成,不打符号的名字,比如GeoLite2-City.mmdb->data.mmdb,不然可能会引发oom。
3.不要调用reader.country();方法,低版本的mmdb文件不支持。

上一篇:7.C++ 命令行编译Cpp


下一篇:Python国际化生成二进制文件时报错: 无效的多字节序列