com.swak.frame.util.NetUtils Maven / Gradle / Ivy
package com.swak.frame.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.*;
import java.util.Enumeration;
import java.util.List;
/**
*
* @Description: NetUtils 获取IP等信息
* @author colley.ma
* @date 2021年3月15日
*/
@Slf4j
public final class NetUtils {
private static final String UNKNOWN = "unknown";
/**
* 获取本机Ip
* @return
*/
public static String localIp() {
String ip = null;
Enumeration> allNetInterfaces;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
List InterfaceAddress = netInterface.getInterfaceAddresses();
for (InterfaceAddress add : InterfaceAddress) {
InetAddress Ip = add.getAddress();
if (Ip != null && Ip instanceof Inet4Address) {
if (StringUtils.equals(Ip.getHostAddress(), "127.0.0.1")) {
continue;
}
ip = Ip.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
log.warn("获取本机Ip失败:异常信息:" + e.getMessage());
}
return ip;
}
public static String getRealIp() {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
try {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && !ip.getHostAddress().contains(":")) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& !ip.getHostAddress().contains(":")) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
} catch (SocketException e) {
log.warn("获取本机Ip失败:异常信息:" + e.getMessage());
throw new RuntimeException(e);
}
}
public static String getClientIpAddr(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.getRemoteAddr();
}
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");
}
// 如果是多级代理,那么取第一个ip为客户ip
if ((ip != null) && (ip.indexOf(',') != -1)) {
ip = ip.substring(ip.lastIndexOf(',') + 1, ip.length()).trim();
}
return ip;
}
public static String getUserAgent(HttpServletRequest request) {
String userAgent = request.getHeader("user-agent");
if (StringUtils.isBlank(userAgent)) {
userAgent = request.getHeader("User-Agent");
}
if (StringUtils.isBlank(userAgent)) {
userAgent = request.getHeader("USER-AGENT");
}
return userAgent;
}
}