开发者社区> 问答> 正文

java如何获取发起请求的用户真实IP

很多业务场景需要用到用户IP,那么该如何去获取到用户的真实IP呢,而不是代理IP呢

展开
收起
阿靖哦 2019-06-11 22:20:50 6147 0
2 条回答
写回答
取消 提交回答
  • /**
         * 获取用户实际IP地址
         * @param request 当前请求对象
         * @return 实际IP地址
         */
        public static String getRemoteIp(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            logger.trace("当前IP来源[X-Forwarded-For], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                //多次反向代理后会有多个ip值,第一个ip才是真实ip
                int index = ip.indexOf(',');
                if(index != -1){
                    return ip.substring(0, index);
                }else{
                    return ip;
                }
            }
            ip = request.getHeader("X-Real-IP");
            logger.trace("当前IP来源[X-Real-IP], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                return ip;
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                logger.trace("当前IP来源[Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                logger.trace("当前IP来源[WL-Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                logger.trace("当前IP来源[HTTP_CLIENT_IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                logger.trace("当前IP来源[HTTP_X_FORWARDED_FOR], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                logger.trace("当前IP来源[getRemoteAddr], 值[{}]", ip);
            }
            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                String ipv4FromLocal = getIpv4FromLocal();
                if (StringUtils.isNotEmpty(ipv4FromLocal)) {
                    ip = ipv4FromLocal;
                }
            }
            return ip;
        }
    
        /**
         * 获取本地IP地址
         * @return IP地址
         */
        private static String getIpv4FromLocal() {
            String ip = null;
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                Process process = Runtime.getRuntime().exec("cmd.exe /c ipconfig | findstr IPv4");
                is = process.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String line = br.readLine();
                ip = line.substring(line.indexOf(':') + 1).trim();
            } catch (IOException e) {
                logger.warn("获取本地IP异常", e);
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    logger.debug("流关闭异常", e);
                }
            }
            return ip;
        }
    
    2019-08-06 09:38:17
    赞同 展开评论 打赏
  • 一个路过的小学生
        protected static String getRemoteAddr(HttpServletRequest request) {
            String caller = request.getHeader("X-Forwarded-For");
            if (caller == null || caller.isEmpty() || caller.equals("unknown")) {
                caller = request.getHeader("Proxy-Client-IP");
            }
    
            if (caller == null || caller.isEmpty() || caller.equals("unknown")) {
                caller = request.getHeader("WL-Proxy-Client-IP");
            }
            
            if (caller == null || caller.isEmpty() || caller.equals("un")) {
                caller = request.getHeader("HTTP_CLIENT_IP");
            }
    
            if (caller == null || caller.isEmpty() || caller.equals("un")) {
                caller = request.getHeader("X-Real-IP");
            }
    
            if (caller == null || caller.isEmpty() || caller.equals("unknown")) {
                caller = request.getRemoteAddr();
            }
            return caller;
        }
    2019-07-17 23:37:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载