• 获取客户端的真实IP


    /**
         * 获取真实IP地址
         * <p>使用getRealIP代替该方法</p>
         * @param request req
         * @return ip
         */
        public static String getClinetIpByReq(HttpServletRequest request) {
            // 获取客户端ip地址
            String clientIp = request.getHeader("x-forwarded-for");
    
            if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
                clientIp = request.getHeader("Proxy-Client-IP");
            }
            if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
                clientIp = request.getHeader("WL-Proxy-Client-IP");
            }
            if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
                clientIp = request.getRemoteAddr();
            }
            /*
             * 对于获取到多ip的情况下,找到公网ip.
             */
            String sIP = null;
            if (clientIp != null && !clientIp.contains("unknown") && clientIp.indexOf(",") > 0) {
                String[] ipsz = clientIp.split(",");
                for (String anIpsz : ipsz) {
                    if (!isInnerIP(anIpsz.trim())) {
                        sIP = anIpsz.trim();
                        break;
                    }
                }
                /*
                 * 如果多ip都是内网ip,则取第一个ip.
                 */
                if (null == sIP) {
                    sIP = ipsz[0].trim();
                }
                clientIp = sIP;
            }
            if (clientIp != null && clientIp.contains("unknown")){
                clientIp =clientIp.replaceAll("unknown,", "");
                clientIp = clientIp.trim();
            }
            if ("".equals(clientIp) || null == clientIp){
                clientIp = "127.0.0.1";
            }
            return clientIp;
        }
    
    
    /**
     * 判断IP是否是内网地址
     * @param ipAddress ip地址
     * @return 是否是内网地址
     */
    public static boolean isInnerIP(String ipAddress) {
        boolean isInnerIp;
        long ipNum = getIpNum(ipAddress);
        /**
         私有IP:A类  10.0.0.0-10.255.255.255
         B类  172.16.0.0-172.31.255.255
         C类  192.168.0.0-192.168.255.255
         当然,还有127这个网段是环回地址
         **/
        long aBegin = getIpNum("10.0.0.0");
        long aEnd = getIpNum("10.255.255.255");
    
        long bBegin = getIpNum("172.16.0.0");
        long bEnd = getIpNum("172.31.255.255");
    
        long cBegin = getIpNum("192.168.0.0");
        long cEnd = getIpNum("192.168.255.255");
        isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd)
                || ipAddress.equals("127.0.0.1");
        return isInnerIp;
    }
    
    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[2]);
        long d = Integer.parseInt(ip[3]);
    
        return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
    }
    
    private static boolean isInner(long userIp, long begin, long end) {
        return (userIp >= begin) && (userIp <= end);
    }
  • 相关阅读:
    CC2431 代码分析⑦
    CC2431 代码分析 ⑤
    CC2431 代码分析⑥
    CC2431 代码分析④-衣锦还乡的CC2431
    基于CC2530/CC2430 的光强采集系统--ADC实验
    Server2012R2 ADFS3.0 The same client browser session has made '6' requests in the last '13'seconds
    Dynamics CRM2013 任务列表添加自定义按钮
    Dynamics CRM 2011/2013 section的隐藏
    Dynamics CRM2013 定制你的系统登录后的首页面
    Dynamics CRM EntityCollection 根据实体中的某个字段为依据去除重复数据
  • 原文地址:https://www.cnblogs.com/jimw/p/9968845.html
Copyright © 2020-2023  润新知