您现在的位置是:群英 > 开发技术 > 编程语言
如何判断ip是否在规定的ip区间,方法是什么
Admin发表于 2022-05-30 17:11:431020 次浏览
这篇文章分享给大家的内容是关于如何判断ip是否在规定的ip区间,方法是什么,本文介绍得很详细,内容很有参考价值,希望可以帮到有需要的小伙伴,接下来就让小编带领大家一起了解看看吧。

思路:

利用ip和ip区间两端的值调用ipIsValid方法。

工具类:

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


public class IPUtil {

	/**
     * 获得当前ip
     * @return
     */
    public static String getIP(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(servletRequestAttributes == null) {
            return "127.0.0.1";
        }

        HttpServletRequest request = servletRequestAttributes.getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        return ip;
    }

    /**
     * 判断IP是否在指定范围
     * @param ipStart
     * @param ipEnd
     * @param ip
     * @return
     */
    public static boolean ipIsValid(String ipStart,String ipEnd, String ip) {
        if (StringUtils.isEmpty(ipStart)) {
            throw new NullPointerException("起始IP不能为空!");
        }
        if (StringUtils.isEmpty(ipEnd)) {
            throw new NullPointerException("结束IP不能为空!");
        }
        if (StringUtils.isEmpty(ip)) {
            throw new NullPointerException("IP不能为空!");
        }
        ipStart = ipStart.trim();
        ipEnd = ipEnd.trim();
        ip = ip.trim();
        final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
        final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
        if (!ipStart.matches(REGX_IP) || !ip.matches(REGX_IP) || !ipEnd.matches(REGX_IP)) {
            return false;
        }
        String[] sips = ipStart.split("\\.");
        String[] sipe = ipEnd.split("\\.");
        String[] sipt = ip.split("\\.");
        long ips = 0L, ipe = 0L, ipt = 0L;
        for (int i = 0; i < 4; ++i) {
            ips = ips << 8 | Integer.parseInt(sips[i]);
            ipe = ipe << 8 | Integer.parseInt(sipe[i]);
            ipt = ipt << 8 | Integer.parseInt(sipt[i]);
        }
        if (ips > ipe) {
            long t = ips;
            ips = ipe;
            ipe = t;
        }
        return ips <= ipt && ipt <= ipe;
    }


}



通过以上内容的阐述,相信大家对“如何判断ip是否在规定的ip区间,方法是什么”已经有了进一步的了解,更多相关的问题,欢迎关注群英网络或到群英官网咨询客服。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: java 判断ip
相关信息推荐
2022-02-15 17:52:25 
摘要:如何利用C语言编写简易的猜数游戏?用C语言实现猜数游戏是一个比较基础的,适合新手学习的一个示例,中文示例代码有一定的参考价值,新手朋友可以参考了解看看,希望对学习C语言的应用有帮助。
2022-06-16 09:25:19 
摘要:转换方法:1、使用explode(),以空格为分割符来拆分字符串并转为数组,语法“explode(" ",$str)”;2、使用“preg_split('/ /',$str,-1,PREG_SPLIT_OFFSET_CAPTURE)”语句。
2022-01-04 18:53:44 
摘要:这篇文章我们来了解Python的numpy安装步骤以及使用方法,本文对大家认识和了解numpy的使用有一定的帮助,有需要的朋友可以了解一些,那么接下来就跟随小编来一起学习看看吧!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部