一般来说,客户端IP通过request.getRemoteAddr()即可得到。但如果客户端通过代理访问后端时,request.getRemoteAddr()可能就不是客户端实际IP,此时需要通过一系列处理来得到客户端实际IP。
package com.origin.util;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author xiye
* @File HttpClientUtil
* @Date 2020/12/10 16:11
* @Desc 网络处理工具
*/
@Slf4j
public class HttpClientUtil {
// 代理请求头名称集合。注意顺序
public static final String[] IP_HEADER = new String[] {"X-Real-IP", "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP"};
/* ========================================================================================= */
/**
* @author xiye
* @date 2020/12/11 16:39
* @param request
* @desc 获取客户端IP
* @return java.lang.String
*/
public static String getRemoteAddress (HttpServletRequest request) {
/*String client_ip = request.getHeader("x-forwarded-for");
if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) {
client_ip = request.getHeader("Proxy-Client-IP");
}
if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) {
client_ip = request.getHeader("WL-Proxy-Client-IP");
}*/
String client_ip = null;
int tag = 0;
for (String header : IP_HEADER) {
client_ip = request.getHeader(header);
if (invalidIp(client_ip)) { tag ++; continue; }
break;
}
if (tag < IP_HEADER.length) log.info("【{}代理IP】{}", IP_HEADER[tag], client_ip);
if(invalidIp(client_ip)) {
client_ip = request.getRemoteAddr();
log.info("【无代理IP】{}", client_ip);
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(client_ip != null && client_ip.length() > 15){ //"***.***.***.***".length() = 15
if(client_ip.indexOf(",") > 0){
client_ip = client_ip.substring(0,client_ip.indexOf(","));
}
}
return client_ip;
}
/**
* @author xiye
* @date 2020/12/11 15:25
* @desc 获取内网IP
* @return java.lang.String
*/
public static String getInnerIp () {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
/* ========================================= 公共 ================================================= */
private static boolean invalidIp (String ip) {
return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
}
}
【参考1】nginx代理转发客户端IP:“X-Real-IP”、“X-Forwarded-For”