1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
package com.ruoyi.framework;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.spring.SpringUtils; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.*; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds;
import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern;
@Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}) }) @Slf4j public class TableFieldInterceptor implements Interceptor {
private final Map<String, List<String>> tableFieldProperties;
public TableFieldInterceptor() { this.tableFieldProperties = SpringUtils.getBean("tableInterceptorPropertiesValue"); }
public static String preprocessSql(String sql) { String regexAES = "AES_DECRYPT\\(UNHEX\\(([^)]+)\\), '([^']+)'\\)(?:\\s+AS\\s+([^,\\s]+))?"; Pattern patternAES = Pattern.compile(regexAES, Pattern.CASE_INSENSITIVE); Matcher matcherAES = patternAES.matcher(sql);
StringBuffer result = new StringBuffer(); while (matcherAES.find()) { String alias = matcherAES.group(3) != null ? matcherAES.group(3).trim() : ""; matcherAES.appendReplacement(result, alias.isEmpty() ? "''" : "'' AS " + alias); } matcherAES.appendTail(result);
String regexConvert = "CONVERT\\(([^)]+)\\s+USING\\s+utf8\\)(?:\\s+AS\\s+([^,\\s]+))?"; Pattern patternConvert = Pattern.compile(regexConvert, Pattern.CASE_INSENSITIVE); Matcher matcherConvert = patternConvert.matcher(result.toString());
result = new StringBuffer(); while (matcherConvert.find()) { String alias = matcherConvert.group(2) != null ? matcherConvert.group(2).trim() : ""; matcherConvert.appendReplacement(result, alias.isEmpty() ? "''" : "'' AS " + alias); } matcherConvert.appendTail(result);
regexAES = "AES_DECRYPT\\(UNHEX\\(([^)]+)\\), '([^']+)'\\)"; patternAES = Pattern.compile(regexAES, Pattern.CASE_INSENSITIVE); matcherAES = patternAES.matcher(result.toString());
result = new StringBuffer(); while (matcherAES.find()) { matcherAES.appendReplacement(result, "''"); } matcherAES.appendTail(result);
regexConvert = "CONVERT\\(([^)]+)\\s+USING\\s+utf8\\)"; patternConvert = Pattern.compile(regexConvert, Pattern.CASE_INSENSITIVE); matcherConvert = patternConvert.matcher(result.toString());
result = new StringBuffer(); while (matcherConvert.find()) { matcherConvert.appendReplacement(result, "''"); } matcherConvert.appendTail(result);
return result.toString(); }
@Override public Object intercept(Invocation invocation) throws Throwable { if (CollUtil.isEmpty(tableFieldProperties)) { return invocation.proceed(); }
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; String sql = mappedStatement.getBoundSql(invocation.getArgs()[1]).getSql();
sql = preprocessSql(sql);
log.warn("{}", sql); SqlParserUtil.SqlDetails sqlDetails = null; try { sqlDetails = SqlParserUtil.parseSql(sql); } catch (Exception e) { e.printStackTrace(); log.error(" 解析失败:id:{},sql{}",mappedStatement.getId(),sql);
throw new RuntimeException(); } log.warn("sqlDetails:{}", sqlDetails);
log.debug("Original SQL: {}", sql);
AtomicBoolean shouldReturnEmpty = new AtomicBoolean(true);
try { String finalSql = sql; SqlParserUtil.SqlDetails finalSqlDetails = sqlDetails; tableFieldProperties.forEach((tableName, conditions) -> { log.warn("{}{}", tableName, conditions); if (StrUtil.containsIgnoreCase(finalSql, tableName)) { conditions.forEach(condition -> { String[] fields = condition.split(","); shouldReturnEmpty.set(false); List< Object> explainedColumns = finalSqlDetails.getTableFieldMap().get(tableName); if (CollUtil.isNotEmpty(explainedColumns) && ArrayUtil.containsAny(explainedColumns.toArray(), fields)) { shouldReturnEmpty.set(true); } if (!shouldReturnEmpty.get()) { throw new ServiceException(""); } }); } });
} catch (Exception ignored) { } if (!shouldReturnEmpty.get()) { log.warn("SQL contains sensitive table and field, returning empty result."); return handleEmptyResult(mappedStatement); }
return invocation.proceed(); }
private Object handleEmptyResult(MappedStatement mappedStatement) { if (mappedStatement.getSqlCommandType().ordinal() == 2) { return 0; } else { return new ArrayList<>(); } }
@Override public Object plugin(Object target) { return Plugin.wrap(target, this); }
@Override public void setProperties(Properties properties) { } }
|