public class IPUtil {
private static final Logger logger = LoggerFactory.getLogger(IPUtil.class);
public static String getRealIP(HttpServletRequest request){
String ipAddress;
try {
ipAddress = request.getHeader("X-Real-IP");
if (check(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (check(ipAddress)){
ipAddress = request.getHeader("X-forwarded-for");
}
if (check(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (check(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (check(ipAddress)) {
// 根据网卡取本机配置的IP
try {
ipAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
// 通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null) {
if (ipAddress.contains(",")) {
return ipAddress.split(",")[0];
} else {
return ipAddress;
}
} else {
return "";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static String getRealAddress(HttpServletRequest request){
String ip = getRealIP(request);
if (ip == null || ip.isEmpty() || "0:0:0:0:0:0:0:1".equals(ip) || "127.0.0.1".equals(ip)){
return null;
}
String address = null;
try {
address = getRealAddress(ip);
} catch (Exception e) {
e.printStackTrace();
}
return address;
}
public static String getRealAddress(String ip) throws Exception{
if (ip == null || ip.equals(""))
return "";
String forObject = new RestTemplate().getForObject("https://qifu-api.baidubce.com/ip/geo/v1/district?ip=" + ip, String.class);
JSONObject json = JSON.parseObject(forObject);
if (json.get("code").equals("Success")) {
StringBuilder builder = new StringBuilder();
JSONObject data = JSON.parseObject(json.get("data").toString());
Object district = data.get("district");
builder.append(data.get("continent"))
.append("-").
append(data.get("country"))
.append("-").
append(data.get("prov"))
.append("-").
append(data.get("city"))
.append(district == null ? "" : "-"+district);
return builder.toString();
}
return "2";
}
public static NotPassGatewayRequestException illegal(HttpServletRequest request){
String realIP = getRealIP(request);
logger.info("realIP:{}",realIP);
if ("0:0:0:0:0:0:0:1".equals(realIP)){
return new NotPassGatewayRequestException(realIP);
}
String realAddress = "";
try {
realAddress = getRealAddress(realIP);
} catch (Exception e) {
e.printStackTrace();
}
return new NotPassGatewayRequestException(realIP + ":" + realAddress);
}
private static boolean check(String ip){
return ip == null || ip.isEmpty() || "unknown".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip) || "127.0.0.1".equals(ip);
}
}
getRealAddress方法获取该ip所在的地理位置,使用百度的免费接口