wisdomisite-java/src/main/java/com/zhgd/mybatis/DataScopeInterceptor.java

163 lines
6.8 KiB
Java
Raw Normal View History

2024-04-23 20:01:26 +08:00
package com.zhgd.mybatis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.zhgd.annotation.DataScope;
2024-04-24 01:15:10 +08:00
import com.zhgd.xmgl.security.util.SecurityUtils;
2024-04-23 20:01:26 +08:00
import lombok.Setter;
2024-04-24 01:15:10 +08:00
import lombok.extern.slf4j.Slf4j;
2024-04-23 20:01:26 +08:00
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.*;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
2024-05-05 22:34:36 +08:00
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
2024-04-23 20:01:26 +08:00
2024-05-05 22:34:36 +08:00
import javax.servlet.http.HttpServletRequest;
2024-04-24 01:15:10 +08:00
import java.lang.reflect.Method;
2024-04-23 20:01:26 +08:00
import java.sql.Connection;
import java.sql.SQLException;
2024-04-24 21:50:28 +08:00
import java.util.Arrays;
2024-04-23 20:01:26 +08:00
import java.util.Map;
2024-04-24 21:50:28 +08:00
import java.util.Objects;
import java.util.Optional;
2024-04-23 20:01:26 +08:00
2024-04-24 01:15:10 +08:00
@Slf4j
2024-04-23 20:01:26 +08:00
public class DataScopeInterceptor extends JsqlParserSupport implements InnerInterceptor {
@Setter
private DataScopeHandler dataScopeHandler;
2024-05-05 22:34:36 +08:00
public static boolean findIgnoreDataScope(Object parameter, DataScope annotation) {
if (annotation == null || !annotation.enable()) {
return true;
}
if (parameter instanceof Map) {
for (Object val : ((Map<?, ?>) parameter).values()) {
if (val instanceof String) {
if (val.equals("ignoreDataScope")) {
return true;
}
}
}
}
if (parameter instanceof String) {
return parameter.equals("ignoreDataScope");
}
return false;
}
2024-04-23 20:01:26 +08:00
public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh);
MappedStatement ms = mpSh.mappedStatement();
SqlCommandType sct = ms.getSqlCommandType();
try {
if (sct == SqlCommandType.INSERT) {
Class<?> clazz = Class.forName(ms.getId().substring(0, ms.getId().lastIndexOf(StringPool.DOT)));
//注解判断
DataScope annotation = clazz.getAnnotation(DataScope.class);
if (annotation == null || annotation.type() == 2) {
return;
}
if (InterceptorIgnoreHelper.willIgnoreTenantLine(ms.getId())) return;
if (SqlParserHelper.getSqlParserInfo(ms)) return;
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
Map param = JSONObject.parseObject(JSON.toJSONString(mpBs.parameterObject()), Map.class);
mpBs.sql(parserMulti(mpBs.sql(), MapUtils.getString(param, "engineeringSn")));
}
} catch (Exception e) {
2024-04-24 01:15:10 +08:00
log.error(e.getMessage(), e);
2024-04-23 20:01:26 +08:00
}
}
@Override
protected void processInsert(Insert insert, int index, String sql, Object obj) {
//dataScopeHandler.addParam(insert, obj);
}
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
try {
2024-04-24 01:15:10 +08:00
if (SecurityUtils.getUser() == null) {
return;
}
2024-04-23 20:01:26 +08:00
Class<?> clazz = Class.forName(ms.getId().substring(0, ms.getId().lastIndexOf(StringPool.DOT)));
2024-04-24 21:50:28 +08:00
String methodName = ms.getId().substring(ms.getId().lastIndexOf(".") + 1);
2024-04-24 01:15:10 +08:00
DataScope annotation = null;
Method[] declaredMethods = clazz.getDeclaredMethods();
2024-04-24 21:50:28 +08:00
Optional<DataScope> dsOption = Arrays.stream(declaredMethods).filter(method -> method.getName().equals(methodName)).map(method -> method.getAnnotation(DataScope.class)).filter(Objects::nonNull).findFirst();
annotation = dsOption.orElseGet(() -> clazz.getAnnotation(DataScope.class));
2024-05-05 22:34:36 +08:00
if (findIgnoreDataScope(parameter, annotation) && isNotSqlTest()) {
2024-04-23 20:01:26 +08:00
return;
}
PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
2024-05-05 22:34:36 +08:00
JSONObject jo = new JSONObject();
jo.put("ds", annotation);
jo.put("parameter", parameter);
mpBs.sql(this.parserSingle(mpBs.sql(), jo));
2024-04-23 20:01:26 +08:00
} catch (Exception e) {
2024-04-24 01:15:10 +08:00
log.error(e.getMessage(), e);
2024-04-23 20:01:26 +08:00
}
}
2024-05-05 22:34:36 +08:00
public static boolean isNotSqlTest() {
2024-05-10 13:00:32 +08:00
try {
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
return request.getParameter("qqq") == null;
} catch (Exception e) {
return true;
}
2024-04-23 20:01:26 +08:00
}
2024-05-05 22:34:36 +08:00
protected void processSelect(Select select, int index, String sql, Object obj) {
this.processSelectBody(select.getSelectBody(), obj);
2024-04-23 20:01:26 +08:00
}
protected void processSelectBody(SelectBody selectBody, Object obj) {
if (selectBody != null) {
if (selectBody instanceof PlainSelect) {
this.processPlainSelect((PlainSelect) selectBody, obj);
} else if (selectBody instanceof WithItem) {
WithItem withItem = (WithItem) selectBody;
this.processSelectBody(withItem.getSelectBody(), obj);
} else {
SetOperationList operationList = (SetOperationList) selectBody;
if (operationList.getSelects() != null && operationList.getSelects().size() > 0) {
this.processSelectBody(operationList.getSelects().get(0), obj);
}
}
}
}
protected void processPlainSelect(PlainSelect plainSelect, Object obj) {
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof Table) {
this.dataScopeHandler.getSqlSegment(plainSelect, obj);
} else {
processFromItem(fromItem, obj);
}
}
protected void processFromItem(FromItem fromItem, Object obj) {
if (fromItem instanceof SubSelect) {
SubSelect subSelect = (SubSelect) fromItem;
if (subSelect.getSelectBody() != null) {
processSelectBody(subSelect.getSelectBody(), obj);
}
}
}
}