zoukankan      html  css  js  c++  java
  • JAVA判断IP是否是内网IP

    /**
         * 私有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这个网段是环回地址
         * localhost  
         */
        static List<Pattern> ipFilterRegexList = new ArrayList<>();
    
        static {
            Set<String> ipFilter = new HashSet<String>();
            ipFilter.add("^10\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])"
                    + "\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])" + "\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$");
            // B类地址范围: 172.16.0.0---172.31.255.255
            ipFilter.add("^172\.(1[6789]|2[0-9]|3[01])\" + ".(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\"
                    + ".(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$");
            // C类地址范围: 192.168.0.0---192.168.255.255
            ipFilter.add("^192\.168\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\"
                    + ".(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$");
            ipFilter.add("127.0.0.1");
            ipFilter.add("0.0.0.0");
            ipFilter.add("localhost");
            for (String tmp : ipFilter) {
                ipFilterRegexList.add(Pattern.compile(tmp));
            }
        }
    
    
    
    /**
         * 判断IP是否内网IP
         * @Title: ipIsInner
         * @param ip
         * @return: boolean
         */
        public static boolean ipIsInner(String ip) {
            boolean isInnerIp = false;
            for (Pattern tmp : ipFilterRegexList) {
                Matcher matcher = tmp.matcher(ip);
                if (matcher.find()) {
                    isInnerIp = true;
                    break;
                }
            }
            return isInnerIp;
        }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    SQL单表查询
    SQL基础
    python生成器yield和send
    python模块
    python异常
    python单例设计模式
    python类方法、类属性和静态方法
    python继承
    react native window下的环境搭建和调试方案
    打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件
  • 原文地址:https://www.cnblogs.com/pxblog/p/13740637.html
Copyright © 2011-2022 走看看