IP区间验证规则修改

beetlsql3-dev
Mlxa0324 2 years ago
parent 098c423bb1
commit 046a63793a

@ -1,6 +1,7 @@
package cn.jlw.util;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReUtil;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.jlw.entity.IpAddress;
import com.ibeetl.jlw.service.IpAddressService;
@ -21,7 +22,6 @@ import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
@ -37,9 +37,11 @@ public class IpUtils {
private static TaskCallable taskCallable = new TaskCallable();
// 分隔符1
public static final String SEPARATOR = ",";
public static final String SEPARATOR = "\\,";
// 分隔符2
public static final String IP_SPLIT_MARK = "-";
public static final String IP_SPLIT_MARK = "\\-";
// 分隔符3
public static final String IP_SPLIT_SEPARATOR = "\\.";
private IpUtils() {
}
@ -114,23 +116,31 @@ public class IpUtils {
}
/**
* IDIP
* IP
*
*
* @param ipAddressList x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.xIP
*/
public static void verifyIpRange(String ipAddressList) {
Assert.notBlank(ipAddressList, "待验证的IP区间不能为空");
for (String ipRange : ipAddressList.split(SEPARATOR)) {
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"));
}
}
/**
* IDIP
*
* @param myIpAddress
* @param ipAddressList x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.xIP
* @return
*/
public static Boolean isRangeInner(String myIpAddress, String ipAddressList) throws PlatformException {
try {
// 单独的验证字符串是否符合要求
Arrays.stream(ipAddressList.split(SEPARATOR)).forEach(item -> {
Assert.isTrue(item.split(IP_SPLIT_MARK).length == 2,
() -> new PlatformException("ipAddressList 参数格式化错误支持格式x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x"));
});
}catch(Exception e) {
throw new PlatformException("ipAddressList 参数格式化错误支持格式x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x");
}
verifyIpRange(ipAddressList);
// 我的IPv4转long类型
long myIpLong = ipv4ToLong(myIpAddress);

@ -1,5 +1,6 @@
package com.ibeetl.jlw.entity;
import cn.hutool.core.util.StrUtil;
import com.ibeetl.admin.core.annotation.Dict;
import com.ibeetl.admin.core.annotation.DictEnum;
import com.ibeetl.admin.core.entity.BaseEntity;
@ -12,6 +13,9 @@ import org.beetl.sql.fetch.annotation.FetchSql;
import javax.validation.constraints.NotNull;
import java.util.Date;
import static cn.jlw.util.IpUtils.verifyIpRange;
import static com.ibeetl.jlw.entity.dto.TeacherOpenCourseStudentSigninLogSigninDTO.TeacherOpenCourseStudentSigninLogTypeEnum.ip_signin;
/*
* ----
* gen by Spring Boot2 Admin 2022-10-11
@ -151,6 +155,11 @@ public class TeacherOpenCourseStudentSigninSetting extends BaseEntity{
*/
public void setTeacherOpenCourseStudentSigninSettingType(String teacherOpenCourseStudentSigninSettingType){
this.teacherOpenCourseStudentSigninSettingType = teacherOpenCourseStudentSigninSettingType;
// 如果是ip签到类型验证IP区间字符
if (StrUtil.isNotBlank(getTeacherOpenCourseStudentSigninSettingValue())
&& getTeacherOpenCourseStudentSigninSettingType().equals(ip_signin.name())) {
verifyIpRange(getTeacherOpenCourseStudentSigninSettingValue());
}
}
/**IP: x.x.x.x-x.x.x.xxxxx
@ -164,6 +173,12 @@ public class TeacherOpenCourseStudentSigninSetting extends BaseEntity{
*/
public void setTeacherOpenCourseStudentSigninSettingValue(String teacherOpenCourseStudentSigninSettingValue){
this.teacherOpenCourseStudentSigninSettingValue = teacherOpenCourseStudentSigninSettingValue;
// 如果是ip签到类型验证IP区间字符
if (StrUtil.isNotBlank(getTeacherOpenCourseStudentSigninSettingValue())
&& getTeacherOpenCourseStudentSigninSettingType().equals(ip_signin.name())) {
verifyIpRange(getTeacherOpenCourseStudentSigninSettingValue());
}
}
/**

Loading…
Cancel
Save