package com.changhong.epc.config.tool;
|
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
|
import java.util.HashSet;
|
import java.util.Set;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 权限过滤工具类
|
*/
|
public class PowerFilterTool {
|
|
/**
|
* 查询表名正则
|
*/
|
public static final Pattern FORM_TABLE_REGEX = Pattern.compile("((?i)from)\\s+\\S+");
|
|
/**
|
* form 关键字 正则
|
*/
|
public static final Pattern FORM_REGEX = Pattern.compile("((?i)from)\\s+");
|
|
/**
|
* 是不是查询语句
|
* @param sql
|
* @return
|
*/
|
public static boolean isSelect(String sql){
|
if(ObjectUtil.empty(sql)){
|
return Boolean.FALSE;
|
}
|
sql = sql.trim();
|
return sql.matches("^((?i)select).+");
|
}
|
|
/**
|
* 查询select的所以表
|
* @param sql
|
* @return
|
*/
|
public static Set<String> getSelectTables(String sql){
|
Matcher matcher = FORM_TABLE_REGEX.matcher(sql);
|
Set<String> talbes = new HashSet<>();
|
String tableName;
|
while (matcher.find()){
|
tableName = FORM_REGEX.matcher(matcher.group()).replaceAll("");
|
tableName = tableName.replace(")", "");
|
// 子表单不控制
|
if(tableName.split("_").length != 4) {
|
talbes.add(tableName);
|
}
|
}
|
return talbes;
|
}
|
|
}
|