ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-10-27 15:31:36  阅读:322  来源: 互联网

标签:return 调用者 mmdb IP private MaxmindGeoIPProperties zh public springboot


背景:
获得接口访问者的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文件不支持。

标签:return,调用者,mmdb,IP,private,MaxmindGeoIPProperties,zh,public,springboot
来源: https://blog.csdn.net/qq_34239851/article/details/120993499

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有