private String getIpAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
//根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
Java 通过Request请求获取IP地址对应省份、城市
项目需要将不同省份的用户,展示不同内容,所以需要通过Request请求获取IP地址, 然后通过IP获取IP对应省份。
这里的操作步骤一共有步:
- 通过Request获取IP
- 通过IP获取对应省份、城市
- 通过设置的省份和IP对应省份进行比对,展示内容
通过Request获取IP
可以参考我的另外一篇文章【Java 通过Request请求获取IP地址】下面是代码:
public class IpAdrressUtil {
/**
* 获取Ip地址
* @param request
* @return
*/
private static String getIpAdrress(HttpServletRequest request) {
String Xip = request.getHeader("X-Real-IP");
String XFor = request.getHeader("X-Forwarded-For");
if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = XFor.indexOf(",");
if(index != -1){
return XFor.substring(0,index);
}else{
return XFor;
}
}
XFor = Xip;
if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
return XFor;
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getRemoteAddr();
}
return XFor;
}
}
通过IP获取对应省份、城市
使用【GeoLite2 City】库
目前开源的IP地址库与城市对应关系用的比较多的是MaxMind公司的GeoLite数据库,GeoLite数据库有开源版本和收费版本,我们使用的是开源版本,GeoLite目前已经更新到2了,所以我们下载GeoLite2 City库。下载地址是【http://dev.maxmind.com/geoip/geoip2/geolite2/】。
如果觉得慢就用迅雷下。下载完成后就是,下载完成就解压。得到【GeoLite2-City.mmdb】文件,这个就是数据库。
Java例子是这样使用的:
首先在项目中加入maven支持
com.maxmind.geoip2
geoip2
2.8.1
然后通过GeoLite2查询得到省份、城市:
public static void main(String[] args) throws Exception{
// 创建 GeoLite2 数据库
File database = new File("/Users/admin/GeoLite2-City.mmdb");
// 读取数据库内容
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName("171.108.233.157");
// 获取查询结果
CityResponse response = reader.city(ipAddress);
// 获取国家信息
Country country = response.getCountry();
System.out.println(country.getIsoCode()); // 'CN'
System.out.println(country.getName()); // 'China'
System.out.println(country.getNames().get("zh-CN")); // '中国'
// 获取省份
Subdivision subdivision = response.getMostSpecificSubdivision();
System.out.println(subdivision.getName()); // 'Guangxi Zhuangzu Zizhiqu'
System.out.println(subdivision.getIsoCode()); // '45'
System.out.println(subdivision.getNames().get("zh-CN")); // '广西壮族自治区'
// 获取城市
City city = response.getCity();
System.out.println(city.getName()); // 'Nanning'
Postal postal = response.getPostal();
System.out.println(postal.getCode()); // 'null'
System.out.println(city.getNames().get("zh-CN")); // '南宁'
Location location = response.getLocation();
System.out.println(location.getLatitude()); // 22.8167
System.out.println(location.getLongitude()); // 108.3167
}
如果是生产环境,可以直接创建一个Service,在Service初始化的时候创建reader对象,然后在公共方法中通过ip查询地址,下面以省份为例:
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.File;
import java.net.InetAddress;
/**
* IP地址服务
*/
@Service
public class IpAddressService {
private static Logger logger = LoggerFactory.getLogger(IpAddressService.class);
private static String dbPath = "/usr/local/GeoLite2-City.mmdb";
private static DatabaseReader reader;
@Autowired
private Environment env;
@PostConstruct
public void init() {
try {
String path = env.getProperty("geolite2.city.db.path");
if (StringUtils.isNotBlank(path)) {
dbPath = path;
}
File database = new File(dbPath);
reader = new DatabaseReader.Builder(database).build();
} catch (Exception e) {
logger.error("IP地址服务初始化异常:" + e.getMessage(), e);
}
}
public String getSubdivision(String ipAddress){
try {
CityResponse response = reader.city(InetAddress.getByName(ipAddress));
return response.getMostSpecificSubdivision().getNames().get("zh-CN");
}catch (Exception e){
logger.error("根据IP[{}]获取省份失败:{}", ipAddress, e.getMessage());
return null;
}
}
}
然后在需要的地方就行判断:
String areaNames = {"北京","天津","上海"};
String subdivision = ipAddressService.getSubdivision(getIpAdrress(request));
// 匹配
if(containsArea(subdivision, areaNames)){
// 处理...
}
匹配方法:
private boolean containsArea(String name, String[] areaNames) {
if(StringUtils.isBlank(name)){
return false;
}
for(String areaName : areaNames){
if(name.contains(areaName)){
return true;
}
}
return false; // 按地域返回数据
}