package com.sandu.ximon.admin.utils;
|
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang.StringUtils;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.net.*;
|
import java.util.Enumeration;
|
|
@Slf4j
|
public class IPUtils {
|
/**
|
* 获取IP地址
|
* <p>
|
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
|
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
|
*/
|
public static String getIpAddr(HttpServletRequest request) {
|
String ip = null;
|
try {
|
ip = request.getHeader("x-forwarded-for");
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (StringUtils.isBlank(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
} catch (Exception e) {
|
log.error("IPUtils ERROR ", e);
|
}
|
|
//使用代理,则获取第一个IP地址
|
if (StringUtils.isBlank(ip) && ip.length() > 15) {
|
if (ip.indexOf(",") > 0) {
|
ip = ip.substring(0, ip.indexOf(","));
|
}
|
}
|
|
return ip;
|
}
|
|
/**
|
* @description 获取系统环境ip
|
*/
|
public static String getCurrentSystemIp() {
|
try {
|
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
while (networkInterfaces.hasMoreElements()) {
|
NetworkInterface ni = networkInterfaces.nextElement();
|
Enumeration<InetAddress> nias = ni.getInetAddresses();
|
while (nias.hasMoreElements()) {
|
InetAddress ia = nias.nextElement();
|
if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
|
return ia.toString().substring(1);
|
}
|
}
|
}
|
} catch (SocketException e) {
|
log.error("Failed to get current system Ip ", e);
|
}
|
return null;
|
}
|
|
|
public static String getIpAddrs(HttpServletRequest request) {
|
String ip = request.getHeader("x-forwarded-for");
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
|
//根据网卡取本机配置的IP
|
InetAddress inet = null;
|
try {
|
inet = InetAddress.getLocalHost();
|
ip = inet.getHostAddress();
|
} catch (UnknownHostException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
}
|
|
if (ip != null && ip.length() > 0) {
|
//取第一个非unknown的有效IP字符串
|
String[] split = ip.split(",");
|
for (String s : split) {
|
if (!"unknown".equalsIgnoreCase(s)) {
|
ip = s;
|
break;
|
}
|
}
|
}
|
|
return ip;
|
}
|
|
public static String getPublicIp() {
|
try {
|
String ip = "http://pv.sohu.com/cityjson?ie=utf-8";
|
URL url = new URL(ip);
|
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
BufferedReader in = new BufferedReader(new
|
InputStreamReader(urlConnection.getInputStream()));
|
StringBuilder stringBuilder = new StringBuilder();
|
String read;
|
while ((read = in.readLine()) != null) {
|
stringBuilder.append(read);
|
}
|
//可打印stringBuilder再解析得到的信息
|
String string = StringUtils.split(stringBuilder.toString(), "=")[1];
|
JSONObject jsonObject = JSONUtil.parseObj(string);
|
return jsonObject.get("cip").toString();
|
} catch (Exception e) {
|
return "未知";
|
}
|
}
|
|
}
|