IP导入功能的验证提交第一版,目前没有对函号,分类性质标签进行验证,ip格式验证的正则表达式填写的是*,后续版本会更新功能。

This commit is contained in:
wangxin
2018-07-02 16:04:41 +08:00
parent 736537bc95
commit 85da517ed0
11 changed files with 319 additions and 13 deletions

View File

@@ -7,6 +7,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -14,6 +16,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jets3t.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
@@ -25,6 +28,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.beust.jcommander.internal.Lists;
import com.nis.domain.FunctionRegionDict;
import com.nis.domain.FunctionServiceDict;
import com.nis.domain.SysDataDictionaryItem;
import com.nis.domain.basics.ServiceDictInfo;
import com.nis.domain.basics.SysDictInfo;
import com.nis.domain.configuration.AreaBean;
@@ -36,6 +40,7 @@ import com.nis.domain.configuration.IpCfgTemplate;
import com.nis.domain.configuration.RequestInfo;
import com.nis.domain.configuration.StringCfgTemplate;
import com.nis.domain.log.BaseLogEntity;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.Configurations;
//import com.nis.main.ConvertTool;
import com.nis.util.Constants;
@@ -533,4 +538,248 @@ public class BaseController {
params.put("searchCfgId", entry.getCfgId());
}
}
public void checkIpCfg(List<IpCfgTemplate> list) throws ServiceException{
Properties prop=this.getMsgProp();
List<SysDataDictionaryItem> ipTypeList = DictUtils.getDictList("IP_TYPE");
List<SysDataDictionaryItem> ipPatternList = DictUtils.getDictList("IP_PATTERN");
List<SysDataDictionaryItem> portPatternList = DictUtils.getDictList("PORT_PATTERN");
List<SysDataDictionaryItem> directionList = DictUtils.getDictList("DIRECTION");
StringBuffer msg=new StringBuffer();
int line=1;
for(IpCfgTemplate value:list){
String lineStart=String.format(prop.getProperty("line"), line)+":";
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;
}
}
if(!has){
errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("ip_type"))+";");
}
}
// 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;
}
}
if(!has){
errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("ip_pattern"))+";");
}
}
// ip_pattern check end
//client_ip check start
String srcIp=value.getSrcIpAddress();
String _msg=checkIP(prop,prop.getProperty("client_ip"),srcIp,ipTypeString,ipPatternString);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//client_ip check end
//server_ip check start
String destIp=value.getDestIpAddress();
if(srcIp.equals(destIp)){
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);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//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;
}
}
if(!has){
errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("port_pattern"))+";");
}
}
//port_pattern check end
//src_port check start
String srcPort=value.getSrcPort();
_msg=checkPort(prop,prop.getProperty("client_port"), srcPort, portPatternString);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//src_port check end
//dest_port check start
String destPort=value.getDestPort();
_msg=checkPort(prop,prop.getProperty("server_port"), destPort, portPatternString);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//dest_port check end
//direction check start
Integer direction=value.getDirection();
if(direction==null){
errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("direction"))+";");
}else{
boolean has=false;
for(SysDataDictionaryItem directionItem:directionList){
if(Integer.parseInt(directionItem.getItemCode())==direction.intValue()){
has=true;
break;
}
}
if(!has){
errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("direction"))+";");
}
}
//direction check end
//protocol check start
Integer protocol= value.getProtocol();
if(protocol==null){
errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("direction"))+";");
}
//direction check end
if(StringUtils.isNotBlank(errInfo.toString())){
msg.append(lineStart).append(errInfo);
if(line<list.size()){
msg.append("<br/>");
}
}
line++;
}
if(StringUtils.isNotBlank(msg.toString())){
throw new MaatConvertException(prop.getProperty("save_failed")+"<br/>"+msg.toString());
}
}
/**
*
* 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,String ipType,String ipPattern){
StringBuffer msg=new StringBuffer();
if(StringUtils.isBlank(ip)){
msg.append(String.format(prop.getProperty("can_not_null"), ipName)+";");
}else{
if("ipv4".equals(ipType)){
if("ip_subnet".equals(ipPattern)){
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("ip_range".equals(ipPattern)){
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("ip".equals(ipPattern)){
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("ipv6".equals(ipType)){
if("ip_subnet".equals(ipPattern)){
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("ip_range".equals(ipPattern)){
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("ip".equals(ipPattern)){
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 checkPort(Properties prop,String portName,String port,String portPattern){
StringBuffer msg=new StringBuffer();
if(StringUtils.isBlank(port)){
msg.append(String.format(prop.getProperty("can_not_null"), portName)+";");
}else{
if("port".equals(portPattern)){
try{
Integer portInt=Integer.parseInt(port);
if(portInt<0||portInt>65535){
msg.append(portName+" "+String.format(prop.getProperty("must_between"), "0","65535")+";");
}
}catch (Exception e) {
// TODO: handle exception
msg.append(String.format(prop.getProperty("not_number"), portName)+";");
}
}else if("port_mask".equals(portPattern)){
if(port.indexOf("/")==-1){
msg.append(String.format(prop.getProperty("is_in_wrong_format"), portName)+";");
}else{
int index=0;
String[] portArray=port.split("/");
for(String portPart:portArray){
try{
Integer portInt=Integer.parseInt(portPart);
if(portInt<0||portInt>65535){
if(index==0){
msg.append(String.format(prop.getProperty("must_between"), portName+" port")+";");
}else{
msg.append(String.format(prop.getProperty("must_between"), portName+" mask")+";");
}
}
}catch (Exception e) {
// TODO: handle exception
if(index==0){
msg.append(String.format(prop.getProperty("not_number"), portName+" port")+";");
}else{
msg.append(String.format(prop.getProperty("not_number"), portName+" mask")+";");
}
}
index++;
}
}
}
}
return msg.toString();
}
}