diff --git a/src/main/java/com/nis/web/controller/BaseController.java b/src/main/java/com/nis/web/controller/BaseController.java index 654034ea7..3d020afa7 100644 --- a/src/main/java/com/nis/web/controller/BaseController.java +++ b/src/main/java/com/nis/web/controller/BaseController.java @@ -724,14 +724,12 @@ public class BaseController { StringBuffer errInfo=new StringBuffer(); // ip_type check start Integer ipType=value.getIpType(); - String ipTypeString=null; if(ipType==null){ errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("ip_type"))+";"); }else{ boolean has=false; for(SysDataDictionaryItem ipTypeItem:ipTypeList){ if(ipType==Integer.parseInt(ipTypeItem.getItemCode())){ - ipTypeString=ipTypeItem.getItemValue(); has=true; break; } @@ -743,14 +741,12 @@ public class BaseController { // ip_type check end // ip_pattern check start Integer ipPattern=value.getIpPattern(); - String ipPatternString=null; if(ipPattern==null){ errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("ip_pattern"))+";"); }else{ boolean has=false; for(SysDataDictionaryItem ipPatternItem:ipPatternList){ if(ipPattern==Integer.parseInt(ipPatternItem.getItemCode())){ - ipPatternString=ipPatternItem.getItemValue(); has=true; break; } @@ -764,9 +760,9 @@ public class BaseController { String srcIp=value.getSrcIpAddress(); String _msg=""; if("ipmulitiplex".equals(specialItem)) { - _msg=checkIP(prop,prop.getProperty("IP"),srcIp,ipTypeString,ipPatternString); + _msg=checkIP(prop,prop.getProperty("IP"),srcIp,ipType,ipPattern); }else { - _msg=checkIP(prop,prop.getProperty("client_ip"),srcIp,ipTypeString,ipPatternString); + _msg=checkIP(prop,prop.getProperty("client_ip"),srcIp,ipType,ipPattern); } if(StringUtils.isNotBlank(_msg)){ errInfo.append(_msg); @@ -776,40 +772,40 @@ public class BaseController { String destIp=value.getDestIpAddress(); if("ipmulitiplex".equals(specialItem)) {//set 默认值 if(ipPattern!=null&&ipType!=null) { - if("ip_subnet".equals(ipPatternString)) { - if("ipv4".equals(ipTypeString)) { + if(1==ipPattern.intValue()) { + if(4==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - }else if("ipv6".equals(ipTypeString)) { + }else if(6==ipType.intValue()) { value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); - }else if("4over6".equals(ipTypeString)) { + }else if(46==ipType.intValue()) { value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); - }else if("6over4".equals(ipTypeString)) { + }else if(64==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - }else if("all".equals(ipTypeString)) { + }else if(10==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); } - }else if("ip_range".equals(ipPatternString)) { - if("ipv4".equals(ipTypeString)) { + }else if(2==ipPattern.intValue()) { + if(4==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - }else if("ipv6".equals(ipTypeString)) { + }else if(6==ipType.intValue()) { value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); - }else if("4over6".equals(ipTypeString)) { + }else if(46==ipType.intValue()) { value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); - }else if("6over4".equals(ipTypeString)) { + }else if(64==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - }else if("all".equals(ipTypeString)) { + }else if(10==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); } - }else if("ip".equals(ipPatternString)) { - if("ipv4".equals(ipTypeString)) { + }else if(3==ipPattern.intValue()) { + if(4==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - }else if("ipv6".equals(ipTypeString)) { + }else if(6==ipType.intValue()) { value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); - }else if("4over6".equals(ipTypeString)) { + }else if(46==ipType.intValue()) { value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); - }else if("6over4".equals(ipTypeString)) { + }else if(64==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - }else if("all".equals(ipTypeString)) { + }else if(10==ipType.intValue()) { value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); } } @@ -819,13 +815,13 @@ public class BaseController { String info=String.format(prop.getProperty("are_the_same"),prop.getProperty("client_ip"),prop.getProperty("server_ip")); errInfo.append(info+";"); } - _msg=checkIP(prop,prop.getProperty("server_ip"),destIp,ipTypeString,ipPatternString); + _msg=checkIP(prop,prop.getProperty("server_ip"),destIp,ipType,ipPattern); if(StringUtils.isNotBlank(_msg)){ errInfo.append(_msg); } } - if(!"ipmulitiplex".equals(specialItem)&&("4over6".equals(ipTypeString)||"6over4".equals(ipTypeString)||"all".equals(ipTypeString))) { - _msg=checkIp(prop, prop.getProperty("client_ip"), srcIp, prop.getProperty("server_ip"), destIp, ipTypeString, ipPatternString); + if(!"ipmulitiplex".equals(specialItem)&&(46==ipType.intValue()||64==ipType.intValue()||10==ipType.intValue())) { + _msg=checkIp(prop, prop.getProperty("client_ip"), srcIp, prop.getProperty("server_ip"), destIp, ipType, ipPattern); if(StringUtils.isNotBlank(_msg)){ errInfo.append(_msg); } @@ -833,14 +829,12 @@ public class BaseController { //server_ip check end //port_pattern check start Integer portPattern=value.getPortPattern(); - String portPatternString=null; if(portPattern==null){ errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("port_pattern"))+";"); }else{ boolean has=false; for(SysDataDictionaryItem portPatternItem:portPatternList){ if(portPattern==Integer.parseInt(portPatternItem.getItemCode())){ - portPatternString=portPatternItem.getItemValue(); has=true; break; } @@ -853,9 +847,9 @@ public class BaseController { //src_port check start String srcPort=value.getSrcPort(); if("ipmulitiplex".equals(specialItem)) { - _msg=checkPort(prop,prop.getProperty("port"), srcPort, portPatternString); + _msg=checkPort(prop,prop.getProperty("port"), srcPort, portPattern); }else { - _msg=checkPort(prop,prop.getProperty("client_port"), srcPort, portPatternString); + _msg=checkPort(prop,prop.getProperty("client_port"), srcPort, portPattern); } if(StringUtils.isNotBlank(_msg)){ @@ -866,14 +860,14 @@ public class BaseController { String destPort=value.getDestPort(); if("ipmulitiplex".equals(specialItem)) {//set 默认值 if(portPattern!=null) { - if("port_mask".equals(portPatternString)) { + if(2==portPattern.intValue()) { value.setDestPort(Constants.PORT_MASK_DEFAULT); - }else if("port".equals(portPatternString)) { + }else if(1==portPattern.intValue()) { value.setDestPort(Constants.PORT_DEFAULT); } } }else { - _msg=checkPort(prop,prop.getProperty("server_port"), destPort, portPatternString); + _msg=checkPort(prop,prop.getProperty("server_port"), destPort, portPattern); } if(StringUtils.isNotBlank(_msg)){ errInfo.append(_msg); @@ -1144,10 +1138,72 @@ public class BaseController { } return msg.toString(); } - public static String checkIp(Properties prop,String srcIpName,String srcIp,String destIpName,String destIp,String ipType,String ipPattern) { + /** + * + * checkIP(IP格式验证) + * (这里描述这个方法适用条件 – 可选) + * @param ipName + * @param ip + * @param ipType + * @param ipPattern + * @return + *String + * @exception + * @since 1.0.0 + */ + public static String checkIP(Properties prop,String ipName,String ip,Integer ipType,Integer ipPattern){ StringBuffer msg=new StringBuffer(); - if("4over6".equals(ipType)){ - if("ip_subnet".equals(ipPattern)){ + if(StringUtils.isBlank(ip)){ + msg.append(String.format(prop.getProperty("can_not_null"), ipName)+";"); + }else{ + if(4==ipType.intValue()){ + if(1==ipPattern.intValue()){ + Pattern pattern=Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); + Matcher matcher=pattern.matcher(ip); + if(!matcher.matches()){//完全匹配 + msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName)+";"); + } + }else if(2==ipPattern.intValue()){ + Pattern pattern=Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); + Matcher matcher=pattern.matcher(ip); + if(!matcher.matches()){//完全匹配 + msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName)+";"); + } + }else if(3==ipPattern.intValue()){ + Pattern pattern=Pattern.compile(Constants.IPV4_IP_REGEXP); + Matcher matcher=pattern.matcher(ip); + if(!matcher.matches()){//完全匹配 + msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName)+";"); + } + } + }else if(6==ipType.intValue()){ + if(1==ipPattern.intValue()){ + Pattern pattern=Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); + Matcher matcher=pattern.matcher(ip); + if(!matcher.matches()){//完全匹配 + msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName)+";"); + } + }else if(2==ipPattern.intValue()){ + Pattern pattern=Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); + Matcher matcher=pattern.matcher(ip); + if(!matcher.matches()){//完全匹配 + msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName)+";"); + } + }else if(3==ipPattern.intValue()){ + Pattern pattern=Pattern.compile(Constants.IPV6_IP_REGEXP); + Matcher matcher=pattern.matcher(ip); + if(!matcher.matches()){//完全匹配 + msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName)+";"); + } + } + } + } + return msg.toString(); + } + public static String checkIp(Properties prop,String srcIpName,String srcIp,String destIpName,String destIp,Integer ipType,Integer ipPattern) { + StringBuffer msg=new StringBuffer(); + if(46==ipType.intValue()){ + if(1==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); Matcher matcherV4=patternV4.matcher(srcIp); @@ -1158,7 +1214,7 @@ public class BaseController { if(!matcherV6.matches()){//完全匹配 msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName)+";"); } - }else if("ip_range".equals(ipPattern)){ + }else if(2==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); Matcher matcherV4=patternV4.matcher(srcIp); @@ -1169,7 +1225,7 @@ public class BaseController { if(!matcherV6.matches()){//完全匹配 msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName)+";"); } - }else if("ip".equals(ipPattern)){ + }else if(3==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_REGEXP); Matcher matcherV4=patternV4.matcher(srcIp); @@ -1181,8 +1237,8 @@ public class BaseController { msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName)+";"); } } - }else if("6over4".equals(ipType)) { - if("ip_subnet".equals(ipPattern)){ + }else if(64==ipType.intValue()) { + if(1==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); Matcher matcherV4=patternV4.matcher(destIp); @@ -1193,7 +1249,7 @@ public class BaseController { if(!matcherV6.matches()){//完全匹配 msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName)+";"); } - }else if("ip_range".equals(ipPattern)){ + }else if(2==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); Matcher matcherV4=patternV4.matcher(destIp); @@ -1204,7 +1260,7 @@ public class BaseController { if(!matcherV6.matches()){//完全匹配 msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName )+";"); } - }else if("ip".equals(ipPattern)){ + }else if(3==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_REGEXP); Matcher matcherV4=patternV4.matcher(destIp); @@ -1216,8 +1272,8 @@ public class BaseController { msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName)+";"); } } - }else if("all".equals(ipType)) { - if("ip_subnet".equals(ipPattern)){ + }else if(10==ipType.intValue()) { + if(1==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); Matcher matcherSrcIpV4=patternV4.matcher(srcIp); @@ -1230,7 +1286,7 @@ public class BaseController { if(!matcherDestIpV4.matches()&&!matcherDestIpV6.matches()){//完全匹配 msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName)+";"); } - }else if("ip_range".equals(ipPattern)){ + }else if(2==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); Matcher matcherSrcIpV4=patternV4.matcher(srcIp); @@ -1243,7 +1299,7 @@ public class BaseController { if(!matcherDestIpV4.matches()&&!matcherDestIpV6.matches()){//完全匹配 msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName)+";"); } - }else if("ip".equals(ipPattern)){ + }else if(3==ipPattern.intValue()){ Pattern patternV4=Pattern.compile(Constants.IPV4_IP_REGEXP); Pattern patternV6=Pattern.compile(Constants.IPV6_IP_REGEXP); Matcher matcherSrcIpV4=patternV4.matcher(srcIp); @@ -1260,12 +1316,12 @@ public class BaseController { } return msg.toString(); } - public static String checkPort(Properties prop,String portName,String port,String portPattern){ + public static String checkPort(Properties prop,String portName,String port,Integer portPattern){ StringBuffer msg=new StringBuffer(); if(StringUtils.isBlank(port)){ msg.append(String.format(prop.getProperty("can_not_null"), portName)+";"); }else{ - if("port".equals(portPattern)){ + if(1==portPattern.intValue()){ try{ Integer portInt=Integer.parseInt(port); if(portInt<0||portInt>65535){ @@ -1275,7 +1331,7 @@ public class BaseController { // TODO: handle exception msg.append(String.format(prop.getProperty("not_number"), portName)+";"); } - }else if("port_mask".equals(portPattern)){ + }else if(2==portPattern.intValue()){ if(port.indexOf("/")==-1){ msg.append(String.format(prop.getProperty("is_in_wrong_format"), portName)+";"); }else{