提交 adb7f3ac authored 作者: 孙洁清's avatar 孙洁清

Merge branch 'master' of git.yfzx.zjtys.com.cn:912-system/monitor/inspect

...@@ -24,7 +24,7 @@ import java.util.List; ...@@ -24,7 +24,7 @@ import java.util.List;
@Aspect @Aspect
@Slf4j @Slf4j
@Component @Component
public class AuthIntercept { public class AopIntercept {
private User user; private User user;
...@@ -32,27 +32,18 @@ public class AuthIntercept { ...@@ -32,27 +32,18 @@ public class AuthIntercept {
this.user = user; this.user = user;
} }
@Pointcut("@annotation(com.zjty.inspect.aop.AuthAnnotation)") @Pointcut("execution(* com.zjty.inspect.inspect.Inspector.inspect(..))")
public void doPointCut() { public void doPointCut() {
} }
@Around("doPointCut()") @Around("doPointCut()")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable { public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
AuthAnnotation annotation = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(AuthAnnotation.class); long startMillis = System.currentTimeMillis();
//注解配置权限code
String[] methodAuthCodes = annotation.code();
//用户拥有权限code
List<String> authorityCode = user.getAuthorityCode();
int i =0;
for (String methodAuthCode : methodAuthCodes) {
if(authorityCode.contains(methodAuthCode)){
i++;
}
}
if(i==0){
return ResponseEntity.status(403).build();
}
Object proceed = joinPoint.proceed(); Object proceed = joinPoint.proceed();
long endMillis = System.currentTimeMillis();
long time = (endMillis - startMillis);
log.info("评估总耗时{}毫秒",time);
log.info("PersonAspect2 ==> before method : {}", joinPoint.getSignature().getName()); log.info("PersonAspect2 ==> before method : {}", joinPoint.getSignature().getName());
log.info("注解的类型名称为{}",joinPoint.getSignature().getDeclaringTypeName()); log.info("注解的类型名称为{}",joinPoint.getSignature().getDeclaringTypeName());
log.info("方法修饰符个数为{}",joinPoint.getSignature().getModifiers()); log.info("方法修饰符个数为{}",joinPoint.getSignature().getModifiers());
......
package com.zjty.inspect.config; package com.zjty.inspect.config;
import com.zjty.inspect.aop.AuthIntercept; import com.zjty.inspect.aop.AopIntercept;
import com.zjty.inspect.entity.ServerResponse; import com.zjty.inspect.entity.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -22,7 +22,7 @@ public class LoginInterceptor implements HandlerInterceptor { ...@@ -22,7 +22,7 @@ public class LoginInterceptor implements HandlerInterceptor {
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Autowired @Autowired
private AuthIntercept authIntercept; private AopIntercept aopIntercept;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
String sessionId = request.getHeader("sessionId"); String sessionId = request.getHeader("sessionId");
...@@ -34,7 +34,7 @@ public class LoginInterceptor implements HandlerInterceptor { ...@@ -34,7 +34,7 @@ public class LoginInterceptor implements HandlerInterceptor {
response.setStatus(403); response.setStatus(403);
return false; return false;
} }
authIntercept.setUser(user.getData()); aopIntercept.setUser(user.getData());
return true; return true;
} }
......
package com.zjty.inspect.inspect; package com.zjty.inspect.inspect;
import com.alibaba.fastjson.JSON;
import com.zjty.inspect.dao.*; import com.zjty.inspect.dao.*;
import com.zjty.inspect.entity.*; import com.zjty.inspect.entity.*;
import com.zjty.inspect.enums.DependenceManagement; import com.zjty.inspect.enums.DependenceManagement;
...@@ -22,31 +21,32 @@ import java.nio.charset.MalformedInputException; ...@@ -22,31 +21,32 @@ import java.nio.charset.MalformedInputException;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.concurrent.CompletableFuture.allOf;
import static java.util.concurrent.CompletableFuture.runAsync;
/** /**
* 项目体检,根据既定特征值, * 项目体检,根据既定特征值,
* 扫描、统计、分析项目特征, * 扫描、统计、分析项目特征,
* 生成报告VO * 生成报告VO
*
* @author mcj
*/ */
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Service @Service
@Slf4j @Slf4j
@Transactional() @Transactional(rollbackFor = Exception.class)
public class Inspector { public class Inspector {
@Autowired
private AnalysisFile analysisFile; private AnalysisFile analysisFile;
@Autowired
private BudgetUitl budgetUitl; private BudgetUitl budgetUitl;
@Autowired
private RuleService ruleService; private RuleService ruleService;
@Autowired
private TechnologyDao technologyDao; private TechnologyDao technologyDao;
@Autowired
private ParameterDao parameterDao; private ParameterDao parameterDao;
private DependencyVo dependencyVo = new DependencyVo(); private DependencyVo dependencyVo = new DependencyVo();
...@@ -125,7 +125,6 @@ public class Inspector { ...@@ -125,7 +125,6 @@ public class Inspector {
*/ */
private Map<String, List<Path>> configFileTypePathsMapping = new HashMap<>(512); private Map<String, List<Path>> configFileTypePathsMapping = new HashMap<>(512);
private Map<String, List<Path>> ruleSuffixFilePathMap; private Map<String, List<Path>> ruleSuffixFilePathMap;
private Map<String, List<Rule>> ruleSuffixMap; private Map<String, List<Rule>> ruleSuffixMap;
...@@ -136,50 +135,97 @@ public class Inspector { ...@@ -136,50 +135,97 @@ public class Inspector {
*/ */
private Map<String, Technology> technologyHashMap = new HashMap<>(64); private Map<String, Technology> technologyHashMap = new HashMap<>(64);
@Autowired
public Inspector(BudgetUitl budgetUitl, RuleService ruleService, TechnologyDao technologyDao, ParameterDao parameterDao, AnalysisFile analysisFile) {
this.budgetUitl = budgetUitl;
this.ruleService = ruleService;
this.technologyDao = technologyDao;
this.parameterDao = parameterDao;
this.analysisFile = analysisFile;
}
/** /**
* 评估 * 评估
* *
* @return 报告 * @return 报告
*/ */
public ReportVo inspect() throws IOException { public ReportVo inspect() throws IOException {
//初始化 //初始化成员变量
initData(); initData();
//扫描文件 //扫描文件,进行文件分类
scanFiles(); scanFiles();
//配置 //配置参数
inspectParameter.setCodeSize((int) codeSize); inspectParameter.setCodeSize((int) codeSize);
report.setFileNum(fileNum); report.setFileNum(fileNum);
report.setFileLine(fileLine); report.setFileLine(fileLine);
log.info("inspect:源代码扫描完成,统计各个文件后缀完成"); log.info("inspect:源代码扫描完成,统计各个文件后缀完成");
//统计项目语言 //统计项目语言
setReportLanguageAndFrame(); CompletableFuture<Void> future = runAsync(this::setReportLanguageAndFrame);
//根据扫描结果以及用户配置得出需要使用的规则及技术 //根据扫描结果以及用户配置得出需要使用的规则及技术
ruleTransform(inspectParameter.getRecastMethod()); CompletableFuture<Void> future1 = runAsync(() -> ruleTransform(inspectParameter.getRecastMethod()));
allOf(future, future1);
//扫描配置文件
forEachFilesMap();
//将得到的告警信息根据技术id进行转换
Set<String> idSet = warns.stream().map(Warn::getTechnologyId).collect(Collectors.toSet());
List<Technology> technologies = technologyDao.findAllById(idSet);
//计算技术金额
Integer technologyFund = 0;
for (Technology tech : technologies) {
technologyFund += tech.getFund();
}
//计算预算
if (inspectParameter.getValid() != null) {
BudgetVo budget = budgetUitl.getBudget(technologyFund, report, inspectParameter);
report.setBudgets(budget);
}
parameterDao.save(inspectParameter);
//填充地址(如果有)
report.setGitAddress(inspectParameter.getGitAddress());
//填充适配技术
report.setTechnologies(technologies);
//填充依赖
report.setDependencyVo(dependencyVo);
//数据转换
HashMap<String, List<Warn>> warnMap = getWarnMap();
ruleService.addRule(rules);
report.setWarnDetails(warnMap);
log.info("评估报告关键技术,{}", warnMap);
return report;
}
/**
* 遍历操作扫描文件后得出的文件
*/
private void forEachFilesMap() {
//解析配置文件集合 //解析配置文件集合
for (Map.Entry<String, List<Path>> entry : configFileTypePathsMapping.entrySet()) { for (Map.Entry<String, List<Path>> entry : configFileTypePathsMapping.entrySet()) {
switch (entry.getKey()) { /**
/** * 配置文件的一个类型,xml文件
* 配置文件的一个类型,xml文件 */
*/ if ("xml".equals(entry.getKey())) {
case "xml": for (Path path : entry.getValue()) {
for (Path path : entry.getValue()) { if (path.getFileName().endsWith("pom.xml")) {
if (path.getFileName().endsWith("pom.xml")) { try {
// TODO: 2020-02-28 解析maven树文件,设置依赖保存到redis // TODO: 2020-02-28 解析maven树文件,设置依赖保存到redis
report.setManager(DependenceManagement.MAVEN.getStatus()); report.setManager(DependenceManagement.MAVEN.getStatus());
ProjectPom projectPom = analysisFile.analysisPom(path); ProjectPom projectPom = analysisFile.analysisPom(path);
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (PomDependency dependency : projectPom.getDependencies()) { for (PomDependency dependency : projectPom.getDependencies()) {
setRule(path, stringBuilder, dependency); setRule(stringBuilder, dependency);
} }
List<String> lines = Files.readAllLines(path); List<String> lines;
lines = Files.readAllLines(path);
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
valiWarn(rules, path, lines.get(i), i + 1); valiWarn(rules, path, lines.get(i), i + 1);
} }
dependencyVo.add(projectPom); dependencyVo.add(projectPom);
} catch (IOException e) {
log.error("路径文件读取错误{}", path.toString());
} }
} }
break; }
default:
} }
} }
//指定后缀到文件匹配关键字 //指定后缀到文件匹配关键字
...@@ -205,33 +251,6 @@ public class Inspector { ...@@ -205,33 +251,6 @@ public class Inspector {
} }
} }
} }
//将得到的告警信息根据技术id进行转换
Set<String> idSet = warns.stream().map(Warn::getTechnologyId).collect(Collectors.toSet());
List<Technology> technologies = technologyDao.findAllById(idSet);
//计算技术金额
Integer technologyFund = 0;
for (Technology tech : technologies) {
technologyFund += tech.getFund();
}
//计算预算
if (inspectParameter.getValid() != null) {
BudgetVo budget = budgetUitl.getBudget(technologyFund, report, inspectParameter);
report.setBudgets(budget);
}
parameterDao.save(inspectParameter);
//填充地址(如果有)
report.setGitAddress(inspectParameter.getGitAddress());
//填充适配技术
report.setTechnologies(technologies);
//填充依赖
report.setDependencyVo(dependencyVo);
//数据转换
HashMap<String, List<Warn>> warnMap = getWarnMap();
ruleService.addRule(rules);
report.setWarnDetails(warnMap);
log.info("评估报告关键技术,{}", warnMap);
return report;
} }
...@@ -267,9 +286,10 @@ public class Inspector { ...@@ -267,9 +286,10 @@ public class Inspector {
/** /**
* rule所需要数据装配 * rule所需要数据装配
*
* @param status 状态 * @param status 状态
* 1:改造 * 1:改造
* 2:适配 * 2:适配
*/ */
private void ruleTransform(Integer status) { private void ruleTransform(Integer status) {
//如果需要改造则查询所有规则 //如果需要改造则查询所有规则
...@@ -309,6 +329,11 @@ public class Inspector { ...@@ -309,6 +329,11 @@ public class Inspector {
} }
private void initData() { private void initData() {
//查询技术,构造支持与非支持技术对象,3个对象
CompletableFuture<Void> future = runAsync(this::findExistTechnology);
//配置语言 map结构
CompletableFuture<Void> future1 = runAsync(this::statisticsLanguage);
CompletableFuture<Void> future2 = runAsync(this::initRule);
codeSize = 0; codeSize = 0;
fileLine = 0; fileLine = 0;
fileNum = 0; fileNum = 0;
...@@ -319,14 +344,10 @@ public class Inspector { ...@@ -319,14 +344,10 @@ public class Inspector {
supportWarns = new ArrayList<>(); supportWarns = new ArrayList<>();
warns.clear(); warns.clear();
rules.clear(); rules.clear();
//查询技术,构造支持与非支持技术对象,3个对象
findExistTechnology();
//配置语言 map结构
statisticsLanguage();
//配置 config文件 结构 //配置 config文件 结构
statisticsConfigFile(); statisticsConfigFile();
initRule();
initTechnology(); initTechnology();
CompletableFuture.allOf(future, future1, future2);
} }
private void statisticsConfigFile() { private void statisticsConfigFile() {
...@@ -413,16 +434,14 @@ public class Inspector { ...@@ -413,16 +434,14 @@ public class Inspector {
warnMap.put(technology.getTechnologyName(), warns1); warnMap.put(technology.getTechnologyName(), warns1);
} }
} }
return warnMap; return warnMap;
} }
/** /**
* @param path 文件路径
* @param stringBuilder string缓冲区 * @param stringBuilder string缓冲区
* @param dependency 依赖 * @param dependency 依赖
*/ */
private void setRule(Path path, StringBuilder stringBuilder, PomDependency dependency) { private void setRule(StringBuilder stringBuilder, PomDependency dependency) {
stringBuilder.append(dependency.getGroupId()).append(":").append(dependency.getArtifactId()); stringBuilder.append(dependency.getGroupId()).append(":").append(dependency.getArtifactId());
Rule rule = new Rule(); Rule rule = new Rule();
...@@ -508,9 +527,10 @@ public class Inspector { ...@@ -508,9 +527,10 @@ public class Inspector {
/** /**
* 扫描源文件 * 扫描源文件
*
* @throws IOException * @throws IOException
*/ */
public void scanFiles() throws IOException { public void scanFiles() {
//以下为计算文件名称匹配正则表达式 //以下为计算文件名称匹配正则表达式
FileSystem aDefault = FileSystems.getDefault(); FileSystem aDefault = FileSystems.getDefault();
Map<String, PathMatcher> languageSuffixMatcherMapping = new HashMap<>(16); Map<String, PathMatcher> languageSuffixMatcherMapping = new HashMap<>(16);
...@@ -531,94 +551,93 @@ public class Inspector { ...@@ -531,94 +551,93 @@ public class Inspector {
//文件读取 //文件读取
if (inspectParameter.getSourceAddress() != null) { if (inspectParameter.getSourceAddress() != null) {
Files.walkFileTree(Paths.get(inspectParameter.getSourceAddress()), new FileVisitor<Path>() { try {
@Override Files.walkFileTree(Paths.get(inspectParameter.getSourceAddress()), new FileVisitor<Path>() {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { @Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
//扫描jar文件时 //扫描jar文件时
if (file.getFileName().toString().endsWith(".jar")) { if (file.getFileName().toString().endsWith(".jar")) {
//新建一个pom对象 //新建一个pom对象
ProjectPom projectPom = new ProjectPom(); ProjectPom projectPom = new ProjectPom();
//截取jar名称 //截取jar名称
String patten = RegexUtil.patten(file.getFileName().toString()); String patten = RegexUtil.patten(file.getFileName().toString());
//新建一个依赖对象 //新建一个依赖对象
PomDependency pomDependency = new PomDependency(); PomDependency pomDependency = new PomDependency();
pomDependency.setArtifactId(patten); pomDependency.setArtifactId(patten);
projectPom.getDependencies().add(pomDependency); projectPom.getDependencies().add(pomDependency);
//当参数为1时代表上传者管理员,代码可绝对信任,将jar名称当作可支持依赖添加进规则库中 //当参数为1时代表上传者管理员,代码可绝对信任,将jar名称当作可支持依赖添加进规则库中
if (inspectParameter.getAdmin() == 1) { if (inspectParameter.getAdmin() == 1) {
//新建规则对象 //新建规则对象
Rule rule = new Rule(); Rule rule = new Rule();
//设置适配技术id //设置适配技术id
rule.setTechnologyId(techJavaSupport.getId()); rule.setTechnologyId(techJavaSupport.getId());
rule.setTarget(patten); rule.setTarget(patten);
//设置文件后缀 //设置文件后缀
rule.setSuffix("*"); rule.setSuffix("*");
rule.setId(UUIDUtil.getUUID()); rule.setId(UUIDUtil.getUUID());
rule.setTechnologyName(techJavaSupport.getTechnologyName()); rule.setTechnologyName(techJavaSupport.getTechnologyName());
//做规则查询,不用去数据库查询 //做规则查询,不用去数据库查询
if (!ruleMap.containsKey(patten + ":" + rule.getSuffix())) { if (!ruleMap.containsKey(patten + ":" + rule.getSuffix())) {
rules.add(rule); rules.add(rule);
ruleMap.put(patten + ":" + rule.getSuffix(), rule); ruleMap.put(patten + ":" + rule.getSuffix(), rule);
}
//设置当前依赖为可支持
pomDependency.setSupport(1);
} else {
//为普通用户上传,依赖需要检查是否支持。
int i = valiWarn(ruleList, file, patten, 0);
//如果值为0则代表是有不支持技术到匹配
pomDependency.setSupport(i);
} }
//设置当前依赖为可支持 dependencyVo.add(projectPom);
pomDependency.setSupport(1);
} else {
//为普通用户上传,依赖需要检查是否支持。
int i = valiWarn(ruleList, file, patten, 0);
//如果值为0则代表是有不支持技术到匹配
pomDependency.setSupport(i);
} }
dependencyVo.add(projectPom); fileNum += 1;
}
fileNum += 1;
try {
List<String> allLines = Files.readAllLines(file); List<String> allLines = Files.readAllLines(file);
fileLine += allLines.size(); fileLine += allLines.size();
} catch (MalformedInputException e) {
return FileVisitResult.CONTINUE; for (Map.Entry<String, PathMatcher> entry : languageSuffixMatcherMapping.entrySet()) {
} catch (IOException e) { //通过正则表达式匹配.java类型后缀文件,并+1
e.printStackTrace(); if (entry.getValue().matches(file)) {
} long length = file.toFile().length();
for (Map.Entry<String, PathMatcher> entry : languageSuffixMatcherMapping.entrySet()) { codeSize += length / 1024;
//通过正则表达式匹配.java类型后缀文件,并+1 languageMatchMap.get(entry.getKey()).plus();
if (entry.getValue().matches(file)) { }
long length = file.toFile().length();
codeSize += length / 1024;
languageMatchMap.get(entry.getKey()).plus();
} }
} for (Map.Entry<PathMatcher, String> entry : configFileMatcherSuffixMapping.entrySet()) {
for (Map.Entry<PathMatcher, String> entry : configFileMatcherSuffixMapping.entrySet()) { //通过配置文件正则表达式匹配.xml文件,记录文件地址
//通过配置文件正则表达式匹配.xml文件,记录文件地址 if (entry.getKey().matches(file)) {
if (entry.getKey().matches(file)) { configFileTypePathsMapping.get(entry.getValue()).add(file);
configFileTypePathsMapping.get(entry.getValue()).add(file); }
} }
} for (Map.Entry<PathMatcher, String> entry : ruleSuffixMap.entrySet()) {
for (Map.Entry<PathMatcher, String> entry : ruleSuffixMap.entrySet()) { //通过规则匹配后缀正则表达式匹配,记录匹配上的文件地址
//通过规则匹配后缀正则表达式匹配,记录匹配上的文件地址 if (entry.getKey().matches(file)) {
if (entry.getKey().matches(file)) { ruleSuffixFilePathMap.get(entry.getValue()).add(file);
ruleSuffixFilePathMap.get(entry.getValue()).add(file); }
} }
return FileVisitResult.CONTINUE;
} }
return FileVisitResult.CONTINUE;
}
@Override @Override
public FileVisitResult visitFileFailed(Path file, IOException exc) { public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@Override @Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) { public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论