mirror of
https://github.com/dataease/dataease.git
synced 2026-05-14 21:12:33 +08:00
perf: 删除存在 SQL 注入风险的代码
This commit is contained in:
@@ -2,7 +2,6 @@ package io.dataease.datasource.dao.ext.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import io.dataease.datasource.dto.CoreDatasourceTaskDTO;
|
||||
import io.dataease.request.GridExample;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -20,7 +19,7 @@ public interface ExtDatasourceTaskMapper {
|
||||
SELECT QRTZ_TRIGGERS.*
|
||||
FROM QRTZ_TRIGGERS
|
||||
${ew.customSqlSegment}
|
||||
"""
|
||||
"""
|
||||
)
|
||||
@Results(
|
||||
id = "taskWithTriggers",
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
package io.dataease.request;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public class BaseGridRequest implements Serializable {
|
||||
|
||||
private String keyword;
|
||||
|
||||
private List<ConditionEntity> conditions;
|
||||
|
||||
private List<String> orders;
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
}
|
||||
|
||||
public void setKeyword(String keyword) {
|
||||
this.keyword = keyword;
|
||||
}
|
||||
|
||||
public List<ConditionEntity> getConditions() {
|
||||
return conditions;
|
||||
}
|
||||
|
||||
public void setConditions(List<ConditionEntity> conditions) {
|
||||
this.conditions = conditions;
|
||||
}
|
||||
|
||||
public List<String> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<String> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public QueryWrapper convertQueryWrapper(QueryWrapper queryWrapper) {
|
||||
if (ObjectUtils.isEmpty(queryWrapper)) {
|
||||
queryWrapper = new QueryWrapper();
|
||||
}
|
||||
for (int i = 0; i < conditions.size(); i++) {
|
||||
ConditionEntity condition = conditions.get(i);
|
||||
String operator = condition.getOperator();
|
||||
Object value = condition.getValue();
|
||||
if (StringUtils.equalsIgnoreCase("eq", operator)) {
|
||||
queryWrapper.eq(condition.getField(), value);
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase("in", operator)) {
|
||||
List list = (List) value;
|
||||
queryWrapper.in(condition.getField(), list);
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase("between", operator)) {
|
||||
List list = (List) value;
|
||||
queryWrapper.between(condition.getField(), list.get(0), list.get(1));
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public GridExample convertExample() {
|
||||
GridExample gridExample = new GridExample();
|
||||
if (!CollectionUtils.isEmpty(conditions)) {
|
||||
GridExample.Criteria criteria = gridExample.createCriteria();
|
||||
conditions.forEach(criteria::addCondition);
|
||||
}
|
||||
|
||||
if (!CollectionUtils.isEmpty(orders)) {
|
||||
String orderByClause = String.join(", ", orders);
|
||||
gridExample.setOrderByClause(orderByClause);
|
||||
}
|
||||
|
||||
return gridExample;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package io.dataease.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class ConditionEntity implements Serializable {
|
||||
|
||||
private String field;
|
||||
|
||||
private String operator;
|
||||
|
||||
private Object value;
|
||||
}
|
||||
@@ -1,302 +0,0 @@
|
||||
package io.dataease.request;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GridExample {
|
||||
protected String orderByClause;
|
||||
|
||||
protected boolean distinct;
|
||||
|
||||
protected List<Criteria> oredCriteria;
|
||||
|
||||
protected String extendCondition;
|
||||
|
||||
public GridExample() {
|
||||
oredCriteria = new ArrayList<Criteria>();
|
||||
}
|
||||
|
||||
public String getExtendCondition() {
|
||||
return extendCondition;
|
||||
}
|
||||
|
||||
public void setExtendCondition(String extendCondition) {
|
||||
this.extendCondition = extendCondition;
|
||||
}
|
||||
|
||||
public void setOrderByClause(String orderByClause) {
|
||||
this.orderByClause = orderByClause;
|
||||
}
|
||||
|
||||
public String getOrderByClause() {
|
||||
return orderByClause;
|
||||
}
|
||||
|
||||
public void setDistinct(boolean distinct) {
|
||||
this.distinct = distinct;
|
||||
}
|
||||
|
||||
public boolean isDistinct() {
|
||||
return distinct;
|
||||
}
|
||||
|
||||
public List<Criteria> getOredCriteria() {
|
||||
return oredCriteria;
|
||||
}
|
||||
|
||||
public void or(Criteria criteria) {
|
||||
oredCriteria.add(criteria);
|
||||
}
|
||||
|
||||
public Criteria or() {
|
||||
Criteria criteria = createCriteriaInternal();
|
||||
oredCriteria.add(criteria);
|
||||
return criteria;
|
||||
}
|
||||
|
||||
public Criteria createCriteria() {
|
||||
Criteria criteria = createCriteriaInternal();
|
||||
if (oredCriteria.size() == 0) {
|
||||
oredCriteria.add(criteria);
|
||||
}
|
||||
return criteria;
|
||||
}
|
||||
|
||||
protected Criteria createCriteriaInternal() {
|
||||
Criteria criteria = new Criteria();
|
||||
return criteria;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
oredCriteria.clear();
|
||||
orderByClause = null;
|
||||
distinct = false;
|
||||
}
|
||||
|
||||
protected abstract static class GeneratedCriteria {
|
||||
protected List<Criterion> criteria;
|
||||
|
||||
protected GeneratedCriteria() {
|
||||
super();
|
||||
criteria = new ArrayList<Criterion>();
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return criteria.size() > 0;
|
||||
}
|
||||
|
||||
public List<Criterion> getAllCriteria() {
|
||||
return criteria;
|
||||
}
|
||||
|
||||
public List<Criterion> getCriteria() {
|
||||
return criteria;
|
||||
}
|
||||
|
||||
protected void addCriterion(String condition) {
|
||||
if (condition == null) {
|
||||
throw new RuntimeException("Value for condition cannot be null");
|
||||
}
|
||||
criteria.add(new Criterion(condition));
|
||||
}
|
||||
|
||||
protected void addNotNullCriterion(String condition) {
|
||||
criteria.add(new Criterion(condition, null));
|
||||
}
|
||||
|
||||
protected void addCriterion(String condition, Object value, String property) {
|
||||
if (value == null) {
|
||||
throw new RuntimeException("Value for " + property + " cannot be null");
|
||||
}
|
||||
criteria.add(new Criterion(condition, value));
|
||||
}
|
||||
|
||||
protected void addSqlCriterion(String condition, Object value, String property) {
|
||||
if (value == null) {
|
||||
throw new RuntimeException("Value for " + property + " cannot be null");
|
||||
}
|
||||
Criterion criterion = new Criterion(condition, value);
|
||||
criterion.sqlValue = true;
|
||||
criteria.add(criterion);
|
||||
}
|
||||
|
||||
protected void addCriterion(String condition, Object value1, Object value2, String property) {
|
||||
if (value1 == null || value2 == null) {
|
||||
throw new RuntimeException("Between values for " + property + " cannot be null");
|
||||
}
|
||||
criteria.add(new Criterion(condition, value1, value2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Criteria addCondition(ConditionEntity conditionEntity){
|
||||
String field = conditionEntity.getField();
|
||||
Object value = conditionEntity.getValue();
|
||||
String operator = conditionEntity.getOperator();
|
||||
if (StringUtils.isEmpty(operator))
|
||||
operator = "like";
|
||||
switch (operator){
|
||||
case "eq":
|
||||
addCriterion(field+" = ", value, field);
|
||||
break;
|
||||
case "ne":
|
||||
addCriterion(field+" <> ", value, field);
|
||||
break;
|
||||
case "like":
|
||||
addCriterion(field+" like ", "%"+value+"%", field);
|
||||
break;
|
||||
case "not like":
|
||||
addCriterion(field+" not like ", "%"+value+"%", field);
|
||||
break;
|
||||
case "in":
|
||||
List<Object> invalues = (List<Object>)value;
|
||||
addCriterion(field+" in", invalues, field);
|
||||
break;
|
||||
case "not in":
|
||||
List<Object> notinvalues = (List<Object>)value;
|
||||
addCriterion(field+" not in", notinvalues, field);
|
||||
break;
|
||||
case "between":
|
||||
List<Object> values = (List<Object>)value;
|
||||
Object v1 = values.get(0);
|
||||
Object v2 = values.get(1);
|
||||
addCriterion(field+" between", v1, v2, field);
|
||||
break;
|
||||
case "gt":
|
||||
addCriterion(field+" > ", value, field);
|
||||
break;
|
||||
case "ge":
|
||||
addCriterion(field+" >= ", value, field);
|
||||
break;
|
||||
case "lt":
|
||||
addCriterion(field+" < ", value, field);
|
||||
break;
|
||||
case "le":
|
||||
addCriterion(field+" <= ", value, field);
|
||||
break;
|
||||
case "not null":
|
||||
addNotNullCriterion(field + " is not null ");
|
||||
break;
|
||||
case "extra":
|
||||
addCriterion(field);
|
||||
break;
|
||||
case "sql in":
|
||||
addCriterion(field+" in ", value, field);
|
||||
break;
|
||||
}
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class Criteria extends GeneratedCriteria {
|
||||
|
||||
protected Criteria() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Criterion {
|
||||
private String condition;
|
||||
|
||||
private Object value;
|
||||
|
||||
private Object secondValue;
|
||||
|
||||
private boolean noValue;
|
||||
|
||||
private boolean singleValue;
|
||||
|
||||
private boolean betweenValue;
|
||||
|
||||
private boolean listValue;
|
||||
|
||||
public boolean isSqlValue() {
|
||||
return sqlValue;
|
||||
}
|
||||
|
||||
public void setSqlValue(boolean sqlValue) {
|
||||
this.sqlValue = sqlValue;
|
||||
}
|
||||
|
||||
private boolean sqlValue;
|
||||
|
||||
private String typeHandler;
|
||||
|
||||
public String getCondition() {
|
||||
return condition;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Object getSecondValue() {
|
||||
return secondValue;
|
||||
}
|
||||
|
||||
public boolean isNoValue() {
|
||||
return noValue;
|
||||
}
|
||||
|
||||
public boolean isSingleValue() {
|
||||
return singleValue;
|
||||
}
|
||||
|
||||
public boolean isBetweenValue() {
|
||||
return betweenValue;
|
||||
}
|
||||
|
||||
public boolean isListValue() {
|
||||
return listValue;
|
||||
}
|
||||
|
||||
public String getTypeHandler() {
|
||||
return typeHandler;
|
||||
}
|
||||
|
||||
protected Criterion(String condition) {
|
||||
super();
|
||||
this.condition = condition;
|
||||
this.typeHandler = null;
|
||||
this.noValue = true;
|
||||
}
|
||||
|
||||
protected Criterion(String condition, Object value, String typeHandler) {
|
||||
super();
|
||||
this.condition = condition;
|
||||
this.value = value;
|
||||
this.typeHandler = typeHandler;
|
||||
if(value == null){
|
||||
this.noValue = true;
|
||||
}else if (value instanceof List<?>) {
|
||||
this.listValue = true;
|
||||
} else {
|
||||
this.singleValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected Criterion(String condition, Object value) {
|
||||
this(condition, value, null);
|
||||
}
|
||||
|
||||
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
|
||||
super();
|
||||
this.condition = condition;
|
||||
this.value = value;
|
||||
this.secondValue = secondValue;
|
||||
this.typeHandler = typeHandler;
|
||||
this.betweenValue = true;
|
||||
}
|
||||
|
||||
protected Criterion(String condition, Object value, Object secondValue) {
|
||||
this(condition, value, secondValue, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package io.dataease.request;
|
||||
|
||||
public interface GridSql {
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="io.dataease.request.GridSql">
|
||||
|
||||
<sql id="gridCondition">
|
||||
<where>
|
||||
<foreach collection="oredCriteria" item="criteria" separator="or">
|
||||
<if test="criteria.valid">
|
||||
<trim prefix="(" prefixOverrides="and" suffix=")">
|
||||
<foreach collection="criteria.criteria" item="criterion">
|
||||
<choose>
|
||||
<when test="criterion.noValue">
|
||||
and ${criterion.condition}
|
||||
</when>
|
||||
<when test="criterion.singleValue">
|
||||
and ${criterion.condition} #{criterion.value}
|
||||
</when>
|
||||
<when test="criterion.betweenValue">
|
||||
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
|
||||
</when>
|
||||
<when test="criterion.listValue">
|
||||
and ${criterion.condition}
|
||||
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
|
||||
#{listItem}
|
||||
</foreach>
|
||||
</when>
|
||||
</choose>
|
||||
</foreach>
|
||||
</trim>
|
||||
</if>
|
||||
</foreach>
|
||||
</where>
|
||||
</sql>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user