ServerHttpRequest 和 HttpServletRequest 获取 IP 地址 学习笔记

ServerHttpRequest 和 HttpServletRequest 获取 IP 地址 学习笔记

最近在做一个请求白名单IP地址的功能;在拦截器 或者 service 层做校验判断,就用到了ServerHttpRequest 和 HttpServletRequest 获取 IP 地址的两种方式
做个记录笔记,如果有不对的地方可以直接私信我

代码

ServerHttpRequest 获取 IP 地址

这个是在拦截器里面用的,我本来是写到公用的的工具包里面的,但是运行会报错,把方法封装到拦截器里面运行又正常了;暂时没去看是什么原因,有知道的大佬,欢迎评论告知下

导包

import java.net.InetAddress;
import java.net.UnknownHostException;

代码

@Component
public class WhiteIpFilter implements GlobalFilter, Ordered {
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		// 请求
		ServerHttpRequest request = exchange.getRequest();
		// 获取 IP 地址
		String ip = getIP(request);
		log.info("========= 请求的IP地址: " + ip);
		return chain.filter(exchange);
	}
	@Override
	public int getOrder() {
		return 0;
	}

	// 多次反向代理后会有多个ip值 的分割符
	private final static String IP_UTILS_FLAG = ",";
	// 未知IP
	private final static String UNKNOWN = "unknown";
	// 本地 IP
	private final static String LOCALHOST_IP = "0:0:0:0:0:0:0:1";
	private final static String LOCALHOST_IP1 = "127.0.0.1";

	private static String getIP(ServerHttpRequest request){
		// 根据 HttpHeaders 获取 请求 IP地址
		String ip = request.getHeaders().getFirst("X-Forwarded-For");
		if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeaders().getFirst("x-forwarded-for");
			if (ip != null && ip.length() != 0 && !UNKNOWN.equalsIgnoreCase(ip)) {
				// 多次反向代理后会有多个ip值,第一个ip才是真实ip
				if (ip.contains(IP_UTILS_FLAG)) {
					ip = ip.split(IP_UTILS_FLAG)[0];
				}
			}
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeaders().getFirst("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeaders().getFirst("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeaders().getFirst("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeaders().getFirst("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeaders().getFirst("X-Real-IP");
		}
		//兼容k8s集群获取ip
		if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddress().getAddress().getHostAddress();
			if (LOCALHOST_IP1.equalsIgnoreCase(ip) || LOCALHOST_IP.equalsIgnoreCase(ip)) {
				//根据网卡取本机配置的IP
				InetAddress iNet = null;
				try {
					iNet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					log.error("getClientIp error: ", e);
				}
				ip = iNet.getHostAddress();
			}
		}
		return ip;
	}
}
HttpServletRequest 获取 IP 地址
	private static final String IP_UTILS_FLAG = ",";
	private static final String UNKNOWN = "unknown";
	private static final String LOCALHOST_IP = "0:0:0:0:0:0:0:1";
	private static final String LOCALHOST_IP1 = "127.0.0.1";
	
	public static String getIpAddr(HttpServletRequest request) {
		String ip = null;
		try {
			//以下两个获取在k8s中,将真实的客户端IP,放到了x-Original-Forwarded-For。而将WAF的回源地址放到了 x-Forwarded-For了。
			ip = request.getHeader("X-Original-Forwarded-For");
			if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getHeader("X-Forwarded-For");
			}
			//获取nginx等代理的ip
			if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getHeader("x-forwarded-for");
			}
			if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (StringUtils.isEmpty(ip) || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			//兼容k8s集群获取ip
			if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
				if (LOCALHOST_IP1.equalsIgnoreCase(ip) || LOCALHOST_IP.equalsIgnoreCase(ip)) {
					//根据网卡取本机配置的IP
					InetAddress iNet = null;
					try {
						iNet = InetAddress.getLocalHost();
					} catch (UnknownHostException e) {
						log.error("getClientIp error: ", e);
					}
					ip = iNet.getHostAddress();
				}
			}
		} catch (Exception e) {
			log.error("IPUtils ERROR ", e);
		}
		//使用代理,则获取第一个IP地址
		if (!StringUtils.isEmpty(ip) && ip.indexOf(IP_UTILS_FLAG) > 0) {
			ip = ip.substring(0, ip.indexOf(IP_UTILS_FLAG));
		}
		return ip;
	}`

你可能感兴趣的:(Java,java)