zoukankan      html  css  js  c++  java
  • java 获取web登录者的ip地址

    /**
         * 获取访问用户的客户端IP(适用于公网与局域网).
         */
        public static final String getIpAddr(final HttpServletRequest request)
                throws Exception {
            if (request == null) {
                throw (new Exception("getIpAddr method HttpServletRequest Object is null"));
            }
            String ipString = request.getHeader("x-forwarded-for");
            if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
                ipString = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
                ipString = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
                ipString = request.getRemoteAddr();
            }
         
            // 多个路由时,取第一个非unknown的ip
            final String[] arr = ipString.split(",");
            for (final String str : arr) {
                if (!"unknown".equalsIgnoreCase(str)) {
                    ipString = str;
                    break;
                }
            }
         
            return ipString;
        }
  • 相关阅读:
    数据库面试题
    MySQL表的导入
    MySQL表的导出
    MySQL安装mydumper
    MySQL中的日志
    动态数组实现下压栈
    动态数组
    设计模式之迭代器
    设计模式之组合模式
    设计模式之状态模式
  • 原文地址:https://www.cnblogs.com/steven-snow/p/7741901.html
Copyright © 2011-2022 走看看