xiatian 11 сар өмнө
parent
commit
0cf6c8baee

+ 17 - 11
examcloud-core-examwork-api-provider/src/main/java/cn/com/qmth/examcloud/core/examwork/api/controller/ExamController.java

@@ -10,8 +10,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -83,6 +81,7 @@ import cn.com.qmth.examcloud.core.examwork.api.controller.bean.ExamDomain;
 import cn.com.qmth.examcloud.core.examwork.api.controller.bean.ExamOrgSettingsDomain;
 import cn.com.qmth.examcloud.core.examwork.api.controller.bean.StudentSpecialSettingsDomain;
 import cn.com.qmth.examcloud.core.examwork.base.enums.ExamProperty;
+import cn.com.qmth.examcloud.core.examwork.base.util.IpUtil;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamCourseRelationRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamOrgPropertyRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamPaperTypeRelationRepo;
@@ -1443,15 +1442,22 @@ public class ExamController extends ControllerSupport {
                 msg.append("  IP地址不能为空");
                 hasError = true;
             } else {
-                String reg = "^(?:(?:1[0-9][0-9]\\.)|(?:2[0-4][0-9]\\.)|(?:25[0-5]\\.)|(?:[1-9][0-9]\\.)|(?:[0-9/*]\\.)){3}(?:(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5])|(?:[1-9][0-9])|(?:[0-9/*]))$";
-                Pattern pattern = Pattern.compile(reg);
-                Matcher matcher = pattern.matcher(ip);
-                if (matcher.find()) {
-                    entity.setIp(ip);
-                } else {
-                    msg.append("  ip格式不正确");
-                    hasError = true;
-                }
+//                String reg = "^(?:(?:1[0-9][0-9]\\.)|(?:2[0-4][0-9]\\.)|(?:25[0-5]\\.)|(?:[1-9][0-9]\\.)|(?:[0-9/*]\\.)){3}(?:(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5])|(?:[1-9][0-9])|(?:[0-9/*]))$";
+//                Pattern pattern = Pattern.compile(reg);
+//                Matcher matcher = pattern.matcher(ip);
+//                if (matcher.find()) {
+//                    entity.setIp(ip);
+//                } else {
+//                    msg.append("  ip格式不正确");
+//                    hasError = true;
+//                }
+            	try {
+					IpUtil.checkIp(ip);
+					entity.setIp(ip);
+				} catch (StatusException e) {
+					msg.append("  "+e.getDesc());
+					hasError = true;
+				}
             }
 
             if (StringUtils.isBlank(line[1])) {

+ 55 - 0
examcloud-core-examwork-base/src/main/java/cn/com/qmth/examcloud/core/examwork/base/util/IpUtil.java

@@ -0,0 +1,55 @@
+package cn.com.qmth.examcloud.core.examwork.base.util;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang3.StringUtils;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+
+public class IpUtil {
+	private static String reg="^0|([1-9][0-9]{0,2})$";
+	public static void checkIp(String ip) {
+		if (StringUtils.isBlank(ip)) {
+			throw new StatusException("ip不能为空");
+		}
+		String[] ss1 = ip.split("\\.");
+		if (ss1.length != 4) {
+			throw new StatusException("ip格式错误");
+		}
+		for (int i = 0; i <= 2; i++) {
+			checkIpNumber(ss1[i]);
+		}
+		String s = ss1[3];
+		if (s.indexOf("-") != -1) {
+			String[] ss2 = ip.split("-");
+			if (ss2.length != 2) {
+				throw new StatusException("ip段格式错误");
+			}
+			for (String s2 : ss2) {
+				checkIpNumber(s2);
+			}
+			if (Integer.valueOf(ss2[0]) >= Integer.valueOf(ss2[1])) {
+				throw new StatusException("ip段格式错误,起始数值必须小于截止数值");
+			}
+		} else {
+			checkIpNumber(s);
+		}
+	}
+
+	private static void checkIpNumber(String s) {
+		Pattern pat = Pattern.compile(reg);
+		Matcher mat = pat.matcher(s);
+		if(!mat.matches()) {
+			throw new StatusException("ip只能是最多三位的整数");
+		}
+		try {
+			int val = Integer.valueOf(s);
+			if (val < 0 || val > 255) {
+				throw new StatusException("ip数值只能是0~255");
+			}
+		} catch (NumberFormatException e) {
+			throw new StatusException("ip只能是整数");
+		}
+	}
+}

+ 2 - 0
examcloud-core-examwork-dao/src/main/java/cn/com/qmth/examcloud/core/examwork/dao/ExamIpLimitRepo.java

@@ -29,5 +29,7 @@ public interface ExamIpLimitRepo extends JpaRepository<ExamIpLimitEntity, Long>,
 	int deleteAllByExamId(Long examId);
 
 	List<ExamIpLimitEntity> findByExamIdAndIp(Long examId, String ip);
+	
+	List<ExamIpLimitEntity> findByExamIdAndLimitType(Long examId, String limitType);
 
 }

+ 57 - 4
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/impl/ExamServiceImpl.java

@@ -50,6 +50,7 @@ import cn.com.qmth.examcloud.core.basic.api.bean.OrgBean;
 import cn.com.qmth.examcloud.core.basic.api.request.GetOrgReq;
 import cn.com.qmth.examcloud.core.basic.api.response.GetOrgResp;
 import cn.com.qmth.examcloud.core.examwork.base.enums.ExamProperty;
+import cn.com.qmth.examcloud.core.examwork.base.util.IpUtil;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamIpLimitRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamOrgPropertyRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamPropertyRepo;
@@ -901,16 +902,18 @@ public class ExamServiceImpl implements ExamService {
             Boolean totalLimit = getExamLimitProperty(examId, ipTotalLimit.getId(), false);
             if (totalLimit) {
                 //全部白名单模式:不在白名单中,才限制访问
-                int count = examIpLimitRepo.countByExamIdAndLimitTypeAndIp(examId, IpLimitType.HAS_ACCESS.name(), realIp);
-                if (count == 0) {
+//                int count = examIpLimitRepo.countByExamIdAndLimitTypeAndIp(examId, IpLimitType.HAS_ACCESS.name(), realIp);
+//                if (count == 0) {
+            	if(checkInIpConfig(realIp, examId, IpLimitType.HAS_ACCESS)) {
                     return true;
                 } else {
                     // return getCenterLimit(manager, examId, accessUser.getUserId(), realIp);
                 }
             } else {
                 //全部黑名单模式:在黑名单中,才限制访问
-                int count = examIpLimitRepo.countByExamIdAndLimitTypeAndIp(examId, IpLimitType.NO_ACCESS.name(), realIp);
-                if (count > 0) {
+//                int count = examIpLimitRepo.countByExamIdAndLimitTypeAndIp(examId, IpLimitType.NO_ACCESS.name(), realIp);
+//                if (count > 0) {
+            	if(!checkInIpConfig(realIp, examId, IpLimitType.NO_ACCESS)) {
                     return true;
                 } else {
                     // return getCenterLimit(manager, examId, accessUser.getUserId(), realIp);
@@ -919,6 +922,55 @@ public class ExamServiceImpl implements ExamService {
         }
         return false;
     }
+    
+    private boolean checkInIpConfig(String realIp, Long examId,IpLimitType type) {
+    	List <ExamIpLimitEntity> ips=examIpLimitRepo.findByExamIdAndLimitType(examId, type.name());
+    	if(CollectionUtils.isEmpty(ips)) {
+    		return false;
+    	}
+    	for(ExamIpLimitEntity ip:ips) {
+    		if(StringUtils.isNotBlank(ip.getIp())) {
+    			if(checkIpEq(realIp, ip.getIp())) {
+    				return true;
+    			}
+    		}
+    	}
+    	return false;
+    }
+    
+    private boolean checkIpEq(String realIp,String ipConfig) {
+    	if(ipConfig.indexOf("-")==-1) {
+    		return realIp.equals(ipConfig);
+    	}else {
+    		String[] ss1=realIp.split("\\.");
+    		String[] ss2=ipConfig.split("\\.");
+    		if(ss1.length!=ss2.length) {
+    			return false;
+    		}
+    		for(int i=0;i<=2;i++) {
+    			if(!ss1[i].equals(ss2[i])) {
+    				return false;
+    			}
+    		}
+    		String[] ssCon=ss2[3].split("-");
+    		if(ssCon.length!=2) {
+    			return false;
+    		}
+    		try {
+    			int val=Integer.valueOf(ss1[3]);
+				int start=Integer.valueOf(ssCon[0]);
+				int end=Integer.valueOf(ssCon[1]);
+				if(start<=val&&val<=end) {
+					return true;
+				}else {
+					return false;
+				}
+			} catch (NumberFormatException e) {
+				return false;
+			}
+     	}
+    }
+    
 
 //    private boolean getCenterLimit(DynamicEnumManager manager, Long examId, Long studentId, String realIp) {
 //        //学习中心访问控制
@@ -958,6 +1010,7 @@ public class ExamServiceImpl implements ExamService {
 
     @Override
     public void saveIpLimit(String ids, ExamIpLimitEntity entity) {
+    	IpUtil.checkIp(entity.getIp());
         //ids不为空,说明是批量设置限制类型
         if (StringUtils.isNotBlank(ids)) {
             List<Long> ipIds = getIdsByPath(ids);

+ 6 - 41
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/impl/OrgIpServiceImpl.java

@@ -33,6 +33,7 @@ import cn.com.qmth.examcloud.core.basic.api.request.GetOrgReq;
 import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
 import cn.com.qmth.examcloud.core.basic.api.response.GetOrgResp;
 import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
+import cn.com.qmth.examcloud.core.examwork.base.util.IpUtil;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.OrgIpRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamEntity;
@@ -117,13 +118,14 @@ public class OrgIpServiceImpl implements OrgIpService {
 		if (req.getOrgId() == null) {
 			throw new StatusException("学习中心不能为空");
 		}
-		checkIp(req.getIp());
+		IpUtil.checkIp(req.getIp());
 		req.setIp(req.getIp().trim());
 		if (req.getRemark() != null && req.getRemark().length() > 100) {
 			throw new StatusException("备注长度不能超过100");
 		}
 		checkIpExists(req);
 		OrgIpEntity e = new OrgIpEntity();
+		e.setOrgId(req.getOrgId());
 		e.setCreationBy(req.getUser().getUserId());
 		e.setUpdateBy(req.getUser().getUserId());
 		e.setIp(req.getIp());
@@ -141,7 +143,7 @@ public class OrgIpServiceImpl implements OrgIpService {
 		if (req.getOrgId() == null) {
 			throw new StatusException("学习中心不能为空");
 		}
-		checkIp(req.getIp());
+		IpUtil.checkIp(req.getIp());
 		req.setIp(req.getIp().trim());
 		if (req.getRemark() != null && req.getRemark().length() > 100) {
 			throw new StatusException("备注长度不能超过100");
@@ -167,44 +169,7 @@ public class OrgIpServiceImpl implements OrgIpService {
 		}
 	}
 
-	private void checkIp(String ip) {
-		if (StringUtils.isBlank(ip)) {
-			throw new StatusException("ip不能为空");
-		}
-		String[] ss1 = ip.split("\\.");
-		if (ss1.length != 4) {
-			throw new StatusException("ip格式错误");
-		}
-		for (int i = 0; i <= 2; i++) {
-			checkIpNumber(ss1[i]);
-		}
-		String s = ss1[3];
-		if (s.indexOf("-") != -1) {
-			String[] ss2 = ip.split("-");
-			if (ss2.length != 2) {
-				throw new StatusException("ip段格式错误");
-			}
-			for (String s2 : ss2) {
-				checkIpNumber(s2);
-			}
-			if (Integer.valueOf(ss2[0]) >= Integer.valueOf(ss2[1])) {
-				throw new StatusException("ip段格式错误,起始数值必须小于截止数值");
-			}
-		} else {
-			checkIpNumber(s);
-		}
-	}
-
-	private void checkIpNumber(String s) {
-		try {
-			int val = Integer.valueOf(s);
-			if (val < 0 || val > 255) {
-				throw new StatusException("ip数值只能是0~255");
-			}
-		} catch (NumberFormatException e) {
-			throw new StatusException("ip只能是整数");
-		}
-	}
+	
 
 	@Transactional
 	@Override
@@ -281,7 +246,7 @@ public class OrgIpServiceImpl implements OrgIpService {
 				hasError = true;
 			} else {
 				try {
-					checkIp(ip);
+					IpUtil.checkIp(ip);
 				} catch (StatusException e) {
 					msg.append("  " + e.getDesc());
 					hasError = true;