From 2cd0f7d0c72d493dcf425e97520c3376db3fc845 Mon Sep 17 00:00:00 2001 From: Mlxa0324 <mlx950324@163.com> Date: Fri, 14 Oct 2022 09:49:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BAIP=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/main/java/cn/jlw/util/IpUtils.java | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/web/src/main/java/cn/jlw/util/IpUtils.java b/web/src/main/java/cn/jlw/util/IpUtils.java index 553f82a0..d67c6336 100644 --- a/web/src/main/java/cn/jlw/util/IpUtils.java +++ b/web/src/main/java/cn/jlw/util/IpUtils.java @@ -119,15 +119,32 @@ public class IpUtils { * 验证IP区间是否符合要求 * * - * @param ipAddressList 支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x。多个IP段逗号隔开 + * @param ipRangeAddressList 支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x。多个IP段逗号隔开 */ - public static void verifyIpRange(String ipAddressList) { - Assert.notBlank(ipAddressList, "待验证的IP区间,不能为空!"); - for (String ipRange : ipAddressList.split(SEPARATOR)) { + public static void verifyIpRange(String ipRangeAddressList) { + Assert.notBlank(ipRangeAddressList, "待验证的IP区间,不能为空!"); + for (String ipRange : ipRangeAddressList.split(SEPARATOR)) { + // IP区间的正则 String reg = "^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}" + "-((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$"; Assert.isTrue(ReUtil.isMatch(reg, ipRange), - () -> new PlatformException("ipAddressList 参数格式化错误!支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x")); + () -> new PlatformException("ipRangeAddressList 参数格式化错误!支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x")); + } + } + + /** + * 验证IP是否符合要求 + * + * + * @param ipAddressList 支持格式:x.x.x.x 多个IP段逗号隔开 + */ + public static void verifyIp(String ipAddressList) { + Assert.notBlank(ipAddressList, "待验证的IP,不能为空!"); + for (String ip : ipAddressList.split(SEPARATOR)) { + // 单IP的正则 + String reg = "^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$"; + Assert.isTrue(ReUtil.isMatch(reg, ip), + () -> new PlatformException("ipAddressList 参数格式化错误!支持格式:x.x.x.x,x.x.x.x")); } }