|
@ -8,12 +8,17 @@ |
|
|
|
|
|
|
|
|
package com.epmet.commons.tools.utils; |
|
|
package com.epmet.commons.tools.utils; |
|
|
|
|
|
|
|
|
|
|
|
import com.epmet.commons.tools.constant.StrConstant; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import java.net.InetAddress; |
|
|
import java.net.InetAddress; |
|
|
|
|
|
import java.net.UnknownHostException; |
|
|
|
|
|
import java.util.Optional; |
|
|
import java.util.regex.Pattern; |
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -24,43 +29,87 @@ import java.util.regex.Pattern; |
|
|
*/ |
|
|
*/ |
|
|
public class IpUtils { |
|
|
public class IpUtils { |
|
|
private static Logger logger = LoggerFactory.getLogger(IpUtils.class); |
|
|
private static Logger logger = LoggerFactory.getLogger(IpUtils.class); |
|
|
public static final String LOCALHOST = "127.0.0.1"; |
|
|
private static final String LOCALHOST = "127.0.0.1"; |
|
|
|
|
|
|
|
|
public static final String ANYHOST = "0.0.0.0"; |
|
|
private static final String ANYHOST = "0.0.0.0"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String IP_UNKNOWN = "unknown"; |
|
|
|
|
|
|
|
|
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); |
|
|
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); |
|
|
|
|
|
|
|
|
public static String getIpAddr(HttpServletRequest request) { |
|
|
public static String getIpAddr(HttpServletRequest request) { |
|
|
|
|
|
|
|
|
String ip = null; |
|
|
String ip = null; |
|
|
try { |
|
|
try { |
|
|
ip = request.getHeader("x-forwarded-for"); |
|
|
ip = request.getHeader("x-forwarded-for"); |
|
|
logger.debug("x-forwarded-for:"+ip); |
|
|
logger.debug("x-forwarded-for:" + ip); |
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { |
|
|
ip = request.getHeader("Proxy-Client-IP"); |
|
|
ip = request.getHeader("Proxy-Client-IP"); |
|
|
} |
|
|
} |
|
|
if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
|
if (StringUtils.isEmpty(ip) || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) { |
|
|
ip = request.getHeader("WL-Proxy-Client-IP"); |
|
|
ip = request.getHeader("WL-Proxy-Client-IP"); |
|
|
} |
|
|
} |
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { |
|
|
ip = request.getHeader("HTTP_CLIENT_IP"); |
|
|
ip = request.getHeader("HTTP_CLIENT_IP"); |
|
|
} |
|
|
} |
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { |
|
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
|
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
|
|
} |
|
|
} |
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { |
|
|
ip = request.getHeader("X-Real-IP"); |
|
|
ip = request.getHeader("X-Real-IP"); |
|
|
} |
|
|
} |
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { |
|
|
ip = request.getRemoteAddr(); |
|
|
ip = request.getRemoteAddr(); |
|
|
} |
|
|
} |
|
|
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
if (ip != null) { //"***.***.***.***".length() = 15
|
|
|
if (ip != null) { //"***.***.***.***".length() = 15
|
|
|
if (ip.indexOf(",") > 0) { |
|
|
if (ip.indexOf(StrConstant.COMMA) > 0) { |
|
|
ip = ip.substring(0, ip.indexOf(",")); |
|
|
ip = ip.split(StrConstant.COMMA)[0]; |
|
|
|
|
|
; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.error("IpUtils getIpAddr ERROR ", e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ip; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getClientIp(ServerHttpRequest request) { |
|
|
|
|
|
String ip = null; |
|
|
|
|
|
try { |
|
|
|
|
|
HttpHeaders headers = request.getHeaders(); |
|
|
|
|
|
String ipAddress = headers.getFirst("x-forwarded-for"); |
|
|
|
|
|
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { |
|
|
|
|
|
ipAddress = headers.getFirst("Proxy-Client-IP"); |
|
|
|
|
|
} |
|
|
|
|
|
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { |
|
|
|
|
|
ipAddress = headers.getFirst("WL-Proxy-Client-IP"); |
|
|
|
|
|
} |
|
|
|
|
|
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { |
|
|
|
|
|
ipAddress = Optional.ofNullable(request.getRemoteAddress()) |
|
|
|
|
|
.map(address -> address.getAddress().getHostAddress()) |
|
|
|
|
|
.orElse(""); |
|
|
|
|
|
if (LOCALHOST.equals(ipAddress)) { |
|
|
|
|
|
// 根据网卡取本机配置的IP
|
|
|
|
|
|
try { |
|
|
|
|
|
InetAddress inet = InetAddress.getLocalHost(); |
|
|
|
|
|
ipAddress = inet.getHostAddress(); |
|
|
|
|
|
} catch (UnknownHostException e) { |
|
|
|
|
|
// ignore
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
|
|
|
if (ip != null) { //"***.***.***.***".length() = 15
|
|
|
|
|
|
if (ip.indexOf(StrConstant.COMMA) > 0) { |
|
|
|
|
|
ip = ip.substring(0, ip.indexOf(StrConstant.COMMA)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return ipAddress; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
logger.error("IpUtils ERROR ", e); |
|
|
logger.error("IpUtils getIpAddr ERROR ", e); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return ip; |
|
|
return ip; |
|
|