协议IP配置增加导入功能

This commit is contained in:
zhangwei
2018-06-11 10:58:06 +08:00
parent 0852cfdef9
commit ed6e14150f
14 changed files with 986 additions and 25 deletions

View File

@@ -1,34 +1,39 @@
package com.nis.web.controller.specific;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Locale;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.nis.domain.ImportErrorInfo;
import com.nis.domain.Page;
import com.nis.domain.specific.SpecificServiceCfg;
import com.nis.domain.specific.SpecificServiceHostCfg;
import com.nis.util.Configurations;
import com.nis.util.StringUtil;
import com.nis.util.excel.ExportExcel;
import com.nis.util.excel.ImportExcel;
import com.nis.web.controller.BaseController;
@Controller
@RequestMapping(value = "${adminPath}/specific/specificServiceHostCfg")
public class SpecificServiceHostCfgController extends BaseController {
@ModelAttribute
public SpecificServiceHostCfg get(@RequestParam(required=false) Integer hostId) {
if (!StringUtil.isEmpty(hostId)){
@@ -68,7 +73,7 @@ public class SpecificServiceHostCfgController extends BaseController {
* @param model
* @return
*/
@RequiresPermissions(value= {"specific:serviceIp:add","specific:serviceIp:edit"},logical=Logical.OR)
@RequiresPermissions("specific:serviceIp:edit")
@RequestMapping(value = "form")
public String form(SpecificServiceHostCfg specificServiceHostCfg, HttpServletRequest request, HttpServletResponse response, Model model) {
//查询协议id供下拉选择
@@ -85,7 +90,7 @@ public class SpecificServiceHostCfgController extends BaseController {
* @param mulitId
* @return
*/
@RequiresPermissions(value= {"specific:serviceIp:add","specific:serviceIp:edit"},logical=Logical.OR)
@RequiresPermissions("specific:serviceIp:edit")
@RequestMapping(value = "saveOrUpdate")
public String saveOrUpdate(SpecificServiceHostCfg specificServiceHostCfg,Model model,RedirectAttributes redirectAttributes){
try {
@@ -104,7 +109,7 @@ public class SpecificServiceHostCfgController extends BaseController {
* @param mulitId
* @return
*/
@RequiresPermissions("specific:serviceIp:del")
@RequiresPermissions("specific:serviceIp:edit")
@RequestMapping(value="delete")
public String delete(SpecificServiceHostCfg specificServiceHostCfg, RedirectAttributes redirectAttributes, String mulitId){
try{
@@ -117,6 +122,80 @@ public class SpecificServiceHostCfgController extends BaseController {
return "redirect:"+adminPath+"/specific/specificServiceHostCfg/list";
}
@RequiresPermissions("specific:serviceIp:edit")
@RequestMapping(value = "import/template")
public String importFileTemplate(HttpServletRequest request,HttpServletResponse response, RedirectAttributes redirectAttributes) {
Properties msgProp = new Properties();
try {
String language = LocaleContextHolder.getLocale().getLanguage();
if(language.equals("zh_cn")||language.equals("zh")){
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_zh_CN.properties"));
}else if(language.equals("ru")){
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_ru.properties"));
}else{
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_en.properties"));
}
} catch (Exception e) {
msgProp = null;
System.err.println("未知i18n消息配置文件,请确定文件是否存在!");
}
try {
String fileName = msgProp.getProperty("agreement_ip_configuration").replaceAll(" ", "_")+".xlsx";
List<SpecificServiceHostCfg> list = Lists.newArrayList();
list.add(new SpecificServiceHostCfg());
new ExportExcel(msgProp,msgProp.getProperty("agreement_ip_configuration"), SpecificServiceHostCfg.class, 2).setDataList(list).
write(request,response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, msgProp.getProperty("import_template_failed")+e.getMessage());
}
return "redirect:"+adminPath+"/specific/specificServiceHostCfg/list";
}
@RequiresPermissions("specific:serviceIp:edit")
@RequestMapping(value = "import", method=RequestMethod.POST)
public String importFile(HttpServletRequest request,Model model,HttpServletResponse response, RedirectAttributes redirectAttributes,
@RequestParam("file") MultipartFile file) {
Properties msgProp = new Properties();
try {
String language = LocaleContextHolder.getLocale().getLanguage();
if(language.equals("zh_cn")||language.equals("zh")){
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_zh_CN.properties"));
}else if(language.equals("ru")){
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_ru.properties"));
}else{
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_en.properties"));
}
} catch (Exception e) {
msgProp = null;
System.err.println("未知i18n消息配置文件,请确定文件是否存在!");
}
List<ImportErrorInfo> errorInfos = null;
try {
ImportExcel ei = new ImportExcel(file, 1, 0);
List<SpecificServiceHostCfg> list = ei.getDataList(SpecificServiceHostCfg.class);
//查询特定服务管理specific_service_cfg协议id供下拉选择
List<SpecificServiceCfg> listSpecService = specificServiceCfgService.findAllSpecificServiceCfg(new SpecificServiceCfg(), "spec_service_id DESC");
errorInfos = specificServiceHostCfgService.importFile(list,listSpecService,msgProp);
// if(result){
// addMessage(redirectAttributes, msgProp.getProperty("import_success"));
// }else{
// addMessage(redirectAttributes, msgProp.getProperty("import_error"));
// }
} catch (Exception e) {
addMessage(redirectAttributes, msgProp.getProperty("import_failed")+errorInfos);
e.printStackTrace();
}
if(errorInfos!=null && errorInfos.size()>0){
addMessage(redirectAttributes, msgProp.getProperty("import_failed")+errorInfos);
}else{
addMessage(redirectAttributes, "success");
}
return "redirect:"+adminPath+"/specific/specificServiceHostCfg/list";
// return renderString(response,errorInfos);
}
/**
* 校验spec_service_id重复
* @param newId

View File

@@ -1,17 +1,23 @@
package com.nis.web.service.specific;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.nis.domain.ImportErrorInfo;
import com.nis.domain.Page;
import com.nis.domain.SysUser;
import com.nis.domain.specific.SpecificServiceCfg;
import com.nis.domain.specific.SpecificServiceHostCfg;
import com.nis.util.BasicProvingUtil;
import com.nis.util.StringUtil;
import com.nis.web.dao.specific.SpecificServiceHostCfgDao;
import com.nis.web.security.UserUtils;
@@ -53,13 +59,15 @@ public class SpecificServiceHostCfgService extends BaseService{
public void saveOrUpdate(SpecificServiceHostCfg specificServiceHostCfg) {
SysUser user = UserUtils.getUser();
String defaultIp = "0.0.0.0"; //缺省0.0.0.0值表示任意
String defaultIpMask = "255.255.255.255";//255.255.255.255表示无掩码
String defaultIpMask = "";
if(specificServiceHostCfg.getIpType().equals(4)){
defaultIpMask = "255.255.255.255"; //255.255.255.255表示无掩码
}
if(specificServiceHostCfg.getIpType().equals(6)){
defaultIp = "::"; //缺省::
defaultIpMask = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; //FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF表示无掩码
}
String defaultPortMask = "65535"; //65535表示无掩码
//ip地址默认 Ipv4缺省0.0.0.0,ipv6缺省为::
//ip地址默认 缺省0.0.0.0值表示任意
if(StringUtil.isBlank(specificServiceHostCfg.getSrcIp())){
specificServiceHostCfg.setSrcIp(defaultIp);
}
@@ -133,6 +141,173 @@ public class SpecificServiceHostCfgService extends BaseService{
return specificServiceHostCfgDao.getBySpecServiceId(specServiceId);
}
/**
* 导入文件
* @param ei
*/
public List<ImportErrorInfo> importFile(List<SpecificServiceHostCfg> list,List<SpecificServiceCfg> listSpecService,Properties msgProp) throws Exception {
List<ImportErrorInfo> importErrorInfos = new ArrayList<ImportErrorInfo>();
//特定服务信息<协议名称,协议ID>
Map<String, Integer> serviceIdMap = new HashMap<String, Integer>();
for (SpecificServiceCfg specificServiceCfg : listSpecService) {
serviceIdMap.put(specificServiceCfg.getSpecServiceName(), specificServiceCfg.getSpecServiceId());
}
for (int i = 0; i < list.size(); i++) {
SpecificServiceHostCfg specificServiceHostCfg = list.get(i);
ImportErrorInfo importErrorInfo = null;
boolean valFlag = true;
SysUser user = UserUtils.getUser();
String defaultIp = "0.0.0.0"; //缺省0.0.0.0值表示任意
String defaultIpMask = "";
//验证
//协议名称=>协议ID
if(StringUtil.isEmpty(specificServiceHostCfg.getSpecServiceName())||!(!StringUtil.isEmpty(specificServiceHostCfg.getSpecServiceName())&&serviceIdMap.containsKey(specificServiceHostCfg.getSpecServiceName()))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_protocol_id"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("protocol"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_protocol_id"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
//throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_protocol_id"));
}else{
specificServiceHostCfg.setSpecServiceId(serviceIdMap.get(specificServiceHostCfg.getSpecServiceName()));
}
//IP类型
if(!(!StringUtil.isEmpty(specificServiceHostCfg.getIpType())&&("4".equals((specificServiceHostCfg.getIpType().toString()))||"6".equals((specificServiceHostCfg.getIpType().toString()))))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_ip_type"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("ip_type"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_ip_type"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_ip_type"));
}
//源IP
if(!StringUtil.isEmpty(specificServiceHostCfg.getSrcIp())&&!BasicProvingUtil.isIpOrIpMask(specificServiceHostCfg.getSrcIp(), specificServiceHostCfg.getIpType())){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_ip"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_ip"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("client_ip"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_ip"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//源IP掩码
if(!StringUtil.isEmpty(specificServiceHostCfg.getSrcIpMask())&&!BasicProvingUtil.isIpOrIpMask(specificServiceHostCfg.getSrcIpMask(), specificServiceHostCfg.getIpType())){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_mask"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_mask"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("client_address_mask"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_mask"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//源端口
if(!(!StringUtil.isEmpty(specificServiceHostCfg.getSrcPort())&&BasicProvingUtil.isPortOrPortMask(specificServiceHostCfg.getSrcPort()))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_port"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_port"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("client_port"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_port"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//源端口掩码
if(!StringUtil.isEmpty(specificServiceHostCfg.getSrcPortMask())&&!BasicProvingUtil.isPortOrPortMask(specificServiceHostCfg.getSrcPortMask())){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_port_mask"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_port_mask"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("client_port_mask"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_src_port_mask"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//目的IP
if(!StringUtil.isEmpty(specificServiceHostCfg.getDstIp())&&!BasicProvingUtil.isIpOrIpMask(specificServiceHostCfg.getDstIp(), specificServiceHostCfg.getIpType())){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_ip"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_ip"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("server_ip"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_ip"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//目的IP掩码
if(!StringUtil.isEmpty(specificServiceHostCfg.getDstIpMask())&&!BasicProvingUtil.isIpOrIpMask(specificServiceHostCfg.getDstIpMask(), specificServiceHostCfg.getIpType())){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_mask"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_mask"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("server_address_mask"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_mask"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//目的端口
if(!(!StringUtil.isEmpty(specificServiceHostCfg.getDstPort())&&BasicProvingUtil.isPortOrPortMask(specificServiceHostCfg.getDstPort()))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_port"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_port"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("server_port"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_port"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//目的端口掩码
if(!(!StringUtil.isEmpty(specificServiceHostCfg.getDstPortMask())&&BasicProvingUtil.isPortOrPortMask(specificServiceHostCfg.getDstPortMask()))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_port_mask"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_port_mask"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("server_port_mask"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_dst_port_mask"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//协议
if(!(!StringUtil.isEmpty(specificServiceHostCfg.getProtocol())&&("6".equals(specificServiceHostCfg.getProtocol().toString())||"17".equals(specificServiceHostCfg.getProtocol().toString())||"0".equals(specificServiceHostCfg.getProtocol().toString())))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_protocol"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_protocol"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("protocol"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_protocol"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//方向
if(!StringUtil.isEmpty(specificServiceHostCfg.getDirection())&&!(("1".equals(specificServiceHostCfg.getDirection().toString())||"0".equals(specificServiceHostCfg.getDirection().toString())))){
logger.info(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_direction"));
// throw new RuntimeException(msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_direction"));
importErrorInfo=new ImportErrorInfo(i+3+"",msgProp.getProperty("direction"),msgProp.getProperty("the_line_of").replace("rowNum", (i+3)+"")+ msgProp.getProperty("val_direction"));
importErrorInfos.add(importErrorInfo);
valFlag = false;
}
//默认值
if(specificServiceHostCfg.getIpType().equals(4)){
defaultIpMask = "255.255.255.255"; //255.255.255.255表示无掩码
}
if(specificServiceHostCfg.getIpType().equals(6)){
defaultIpMask = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; //FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF表示无掩码
}
String defaultPortMask = "65535"; //65535表示无掩码
//ip地址默认 缺省0.0.0.0值表示任意
if(StringUtil.isBlank(specificServiceHostCfg.getSrcIp())){
specificServiceHostCfg.setSrcIp(defaultIp);
}
if(StringUtil.isBlank(specificServiceHostCfg.getDstIp())){
specificServiceHostCfg.setDstIp(defaultIp);
}
//ip掩码默认
if(StringUtil.isBlank(specificServiceHostCfg.getSrcIpMask())){
specificServiceHostCfg.setSrcIpMask(defaultIpMask);
}
if(StringUtil.isBlank(specificServiceHostCfg.getDstIpMask())){
specificServiceHostCfg.setDstIpMask(defaultIpMask);
}
//端口掩码默认
if(StringUtil.isBlank(specificServiceHostCfg.getSrcPortMask())){
specificServiceHostCfg.setSrcPortMask(defaultPortMask);
}
if(StringUtil.isBlank(specificServiceHostCfg.getDstPortMask())){
specificServiceHostCfg.setDstPortMask(defaultPortMask);
}
//方向缺省
if(specificServiceHostCfg.getDirection()==null){
specificServiceHostCfg.setDirection(0);
}
Date date = new Date();
specificServiceHostCfg.setIsValid(1);
specificServiceHostCfg.setCreator(user);
specificServiceHostCfg.setCreateTime(date);
// specificServiceHostCfg.setEditor(user);
// specificServiceHostCfg.setEditTime(date);
// specificServiceHostCfg.setAuditor(user);
// specificServiceHostCfg.setAuditTime(date);
if (valFlag) {
specificServiceHostCfgDao.insert(specificServiceHostCfg);
}
}
return importErrorInfos;
}
}