1、新增com.alibaba:druid依赖
2、动态规则类新增sql解析方法,发送dynamicTaskInfos时对sql进行解析。 3、ResponseResult返回响应信息修改为中文 4、动态任务运行状态转变为运行中时,dynamicTaskInfos生成的查询mapper语句修改
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
package com.realtime.protection.configuration.entity.task;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Data
|
||||
public class DynamicTaskInfo {
|
||||
|
||||
@@ -45,10 +55,76 @@ public class DynamicTaskInfo {
|
||||
@JsonProperty("event_type")
|
||||
private String eventType;
|
||||
|
||||
@JsonProperty("log_rule_id")
|
||||
private Long logRuleId;
|
||||
// @JsonProperty("event_ids")
|
||||
// private List<Long> eventIds;
|
||||
//
|
||||
// @JsonProperty("c_inoutid")
|
||||
// private List<Long> inoutId;
|
||||
//
|
||||
// @JsonProperty("c_netnum")
|
||||
// private List<Long> netNum;
|
||||
|
||||
@JsonProperty("conditions")
|
||||
private HashMap<String,String> conditions = new HashMap<>();
|
||||
|
||||
// @JsonProperty("eq_condition")
|
||||
// private HashMap<String,String> eqConditions = new HashMap<>();;
|
||||
|
||||
@JsonProperty("table_name")
|
||||
private List<String> tableNames = new ArrayList<>();
|
||||
|
||||
@JsonProperty("bw_sql")
|
||||
private String bwSql;
|
||||
|
||||
@JsonProperty("select_columns")
|
||||
private List<String> selectColumns = new ArrayList<>();
|
||||
|
||||
// 从防护对象列表中获取
|
||||
@JsonProperty("protect_objects")
|
||||
private List<SimpleProtectObject> protectObjects;
|
||||
|
||||
public void parseSql() {
|
||||
String bwSql = this.bwSql;
|
||||
//解析SQL语句
|
||||
SQLStatementParser parser = new MySqlStatementParser(bwSql);
|
||||
// 使用Parser解析生成AST,这里SQLStatement就是AST
|
||||
SQLStatement sqlStatement = parser.parseStatement();
|
||||
|
||||
// 检查是否是SELECT语句
|
||||
if (sqlStatement instanceof SQLSelectStatement) {
|
||||
// 创建访问者
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
sqlStatement.accept(visitor);
|
||||
|
||||
// 进一步细化,确保只获取SELECT子句的字段
|
||||
SQLSelectStatement selectStatement = (SQLSelectStatement) sqlStatement;
|
||||
SQLSelect select = selectStatement.getSelect();
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
|
||||
|
||||
queryBlock.getSelectList().forEach(item -> this.selectColumns.add(item.toString()));
|
||||
|
||||
Map<TableStat.Name, TableStat> tables = visitor.getTables();
|
||||
for (TableStat.Name t : tables.keySet()) {
|
||||
this.tableNames.add(t.getName());
|
||||
}
|
||||
//获取where条件
|
||||
List<TableStat.Condition> conditions = visitor.getConditions();
|
||||
conditions.forEach(condition -> {
|
||||
// log.info("解析sql后的查询条件:{}", condition.getColumn().getName());
|
||||
if (condition.getOperator().equals("IN")){
|
||||
this.conditions.put(condition.getColumn().getName(),condition.getValues().toString());
|
||||
}
|
||||
if (condition.getOperator().equals("=")){
|
||||
this.conditions.put(condition.getColumn().getName(),condition.getValues().toString());
|
||||
}
|
||||
});
|
||||
// log.info("解析sql后的查询条件:{}",conditions);
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("不是SELECT查询语句,不进行字段解析。");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ResponseResult implements Serializable {
|
||||
}
|
||||
|
||||
public static ResponseResult ok() {
|
||||
return new ResponseResult(200, "request succeed");
|
||||
return new ResponseResult(200, "请求成功");
|
||||
}
|
||||
|
||||
public static ResponseResult ok(String message) {
|
||||
@@ -49,11 +49,11 @@ public class ResponseResult implements Serializable {
|
||||
}
|
||||
|
||||
public static ResponseResult error() {
|
||||
return new ResponseResult(500, "request failed");
|
||||
return new ResponseResult(500, "请求失败");
|
||||
}
|
||||
|
||||
public static ResponseResult invalid() {
|
||||
return new ResponseResult(400, "invalid request");
|
||||
return new ResponseResult(400, "请求无效");
|
||||
}
|
||||
|
||||
public static ResponseResult invalid(String message) {
|
||||
@@ -61,7 +61,7 @@ public class ResponseResult implements Serializable {
|
||||
}
|
||||
|
||||
public static ResponseResult unAuthorized() {
|
||||
return new ResponseResult(401, "UnAuthorized User");
|
||||
return new ResponseResult(401, "未授权用户");
|
||||
}
|
||||
|
||||
public static ResponseResult error(String message) {
|
||||
|
||||
@@ -92,7 +92,7 @@ public class TaskService {
|
||||
//校验防护对象是否存在
|
||||
boolean ProtectObjIdValid = task.getProtectObjectIds().stream()
|
||||
.allMatch(dynamicRuleMapper::queryProtectObjectById);
|
||||
if (!ProtectObjIdValid) {
|
||||
if (!ProtectObjIdValid && !task.getProtectObjectIds().isEmpty()){
|
||||
throw new IllegalArgumentException("部分防护对象不存在");
|
||||
}
|
||||
//任务和防护对象多对多关联建立
|
||||
|
||||
@@ -8,11 +8,10 @@ import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
|
||||
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClientResponseException;
|
||||
import org.springframework.web.reactive.function.client.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
@@ -114,12 +113,18 @@ public class StateHandler {
|
||||
if (dynamicTaskInfos == null || dynamicTaskInfos.isEmpty()) {
|
||||
throw new IllegalArgumentException("动态规则列表为空,请至少选择一个动态规则以启动动态/研判后类型任务");
|
||||
}
|
||||
//解析sql
|
||||
dynamicTaskInfos.forEach(dynamicTaskInfo -> {
|
||||
dynamicTaskInfo.parseSql();
|
||||
});
|
||||
|
||||
// 将所有关联的动态规则审批状态修改为“已使用”
|
||||
taskService.updateDynamicRuleAuditStatusInTask(task.getTaskId(), AuditStatusEnum.USING);
|
||||
|
||||
AtomicReference<Boolean> success = new AtomicReference<>(false);
|
||||
|
||||
// System.out.println(dynamicTaskInfos);
|
||||
|
||||
Mono<SimpleResponse> mono = client.post()
|
||||
.uri("/api/v1/kafkasend")
|
||||
.bodyValue(dynamicTaskInfos)
|
||||
@@ -144,3 +149,4 @@ public class StateHandler {
|
||||
return success.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user