获取本机请求时真实ip

前言

有时候我们需要调用别人的接口,需要对自己的真实ip加入白名单才能调通,但是请求发出后会经过层层代理,导致我们不知道自己请求的真实ip,下面这个方法可以拿到

package com.sinosoft.springbootplus.lft.business.dispatch.grid.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class test {

        private static final Logger logger = LoggerFactory.getLogger(test.class);

        private static final String localIp = "127.0.0.1";

        /**
         * 获取用户真实IP地址,不使用request.getRemoteAddr()的原因是有可能用户使用了代理软件方式避免真实IP地址,
         * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值
         *
         * @return ip
         */
        public static String getIpAddress(HttpServletRequest request) {
            String ipAddress;
            try {
                ipAddress = request.getHeader("x-forwarded-for");
                if (ipAddress != null && ipAddress.length() != 0 && !"unknown".equalsIgnoreCase(ipAddress)) {
                    // 多次反向代理后会有多个ip值,第一个ip才是真实ip
                    if (ipAddress.indexOf(",") != -1) {
                        ipAddress = ipAddress.split(",")[0];
                        logger.info("多次反向代理后 ip: " + ipAddress);
                    }
                }
                if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                    ipAddress = request.getHeader("Proxy-Client-IP");
                    logger.info("Proxy-Client-IP ip: " + ipAddress);
                }
                if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                    ipAddress = request.getHeader("WL-Proxy-Client-IP");
                    logger.info("WL-Proxy-Client-IP ip: " + ipAddress);
                }
                if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                    ipAddress = request.getHeader("HTTP_CLIENT_IP");
                    logger.info("HTTP_CLIENT_IP ip: " + ipAddress);
                }
                if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                    ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
                    logger.info("HTTP_X_FORWARDED_FOR ip: " + ipAddress);
                }
                if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                    ipAddress = request.getHeader("X-Real-IP");
                    logger.info("X-Real-IP ip: " + ipAddress);
                }
                if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                    ipAddress = request.getRemoteAddr();
                    if (localIp.equals(ipAddress)) {
                        // 根据网卡取本机配置的IP
                        InetAddress inet = null;
                        try {
                            inet = InetAddress.getLocalHost();
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        }
                        assert inet != null;
                        ipAddress = inet.getHostAddress();
                        logger.info("根据网卡取本机配置 ip: " + ipAddress);
                    }
                }
                // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
                if (ipAddress != null && ipAddress.length() > 15) {
                    // = 15
                    if (ipAddress.indexOf(",") > 0) {
                        ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                        logger.info("多次反向代理后 ip: " + ipAddress);
                    }
                }
            } catch (Exception e) {
                ipAddress = "";
            }
            logger.info("IP地址信息:" + ipAddress);
            return "0:0:0:0:0:0:0:1".equals(ipAddress) ? localIp : ipAddress;
        }
}

你可能感兴趣的:(tcp/ip,网络协议,网络)