当前位置:   article > 正文

获取本机请求时真实ip

获取本机请求时真实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;
        }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/50340
推荐阅读
相关标签
  

闽ICP备14008679号