diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/DataScopeInnerInterceptor.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/DataScopeInnerInterceptor.java index 7ebaf824209eeb66bad6bc791c632012c95a20ef..3e69a74295d02b7c1ad19a1c5c2a6aa7fa8310b5 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/DataScopeInnerInterceptor.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/DataScopeInnerInterceptor.java @@ -13,9 +13,11 @@ import lombok.extern.slf4j.Slf4j; import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.expression.Alias; import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.ExpressionVisitorAdapter; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.parser.CCJSqlParserManager; import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.schema.Column; import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.statement.select.*; import org.apache.ibatis.executor.Executor; @@ -29,7 +31,6 @@ import org.springframework.web.util.pattern.PathPatternParser; import java.io.StringReader; import java.sql.SQLException; import java.util.*; -import java.util.stream.Collectors; import static com.central.common.datascope.mp.sql.handler.SqlHandler.ALIAS_SYNBOL; @@ -66,7 +67,7 @@ public class DataScopeInnerInterceptor implements InnerInterceptor { } @Override - public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { if(CollUtil.isEmpty(dataScopeProperties.getIgnoreSqls())|| !dataScopeProperties.getIgnoreSqls().contains(ms.getId())){ PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); String sql = boundSql.getSql(); @@ -227,19 +228,26 @@ public class DataScopeInnerInterceptor implements InnerInterceptor { * @throws JSQLParserException */ private SelectBody reformWhere(PlainSelect select, String whereSql, List aliasName) throws JSQLParserException { - Expression where = select.getWhere(); + // todo 处理exists if(StrUtil.isNotBlank(whereSql)&& CollUtil.isNotEmpty(aliasName)){ - String andWhereSql = aliasName.stream() - .map(item -> whereSql.replaceAll(ALIAS_SYNBOL, StrUtil.isNotBlank(item) ? item : "")) - .collect(Collectors.joining(" and ")); - if(StrUtil.isNotBlank(andWhereSql)){ + for (String alias : aliasName) { Expression expression = CCJSqlParserUtil - .parseCondExpression(andWhereSql); - if(ObjectUtil.isNull(where)){ + .parseCondExpression(whereSql); + expression.accept(new ExpressionVisitorAdapter(){ + @Override + public void visit(Column column) { + if(Objects.isNull(column.getTable())|| ALIAS_SYNBOL.equals(column.getTable().toString())){ + Table table = new Table(); + table.setAlias(new Alias(alias)); + column.setTable(table); + } + } + }); + if(ObjectUtil.isNull(select.getWhere())){ select.setWhere(expression); }else { - AndExpression andExpression = new AndExpression(where, expression); + AndExpression andExpression = new AndExpression(select.getWhere(), expression); select.setWhere(andExpression); } } diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/EnableQuerySqlLogInnerInterceptor.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/EnableQuerySqlLogInnerInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..b23ceed6538457ed3f4b1b9d005a2a2fd35530cd --- /dev/null +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/interceptor/EnableQuerySqlLogInnerInterceptor.java @@ -0,0 +1,37 @@ +package com.central.common.datascope.mp.interceptor; + +import cn.hutool.core.lang.Assert; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.sql.SQLException; + +/** + * 示例 + * + * @author jarvis create by 2023/2/2 + */ +@Slf4j +public class EnableQuerySqlLogInnerInterceptor implements InnerInterceptor{ + private InnerInterceptor delegate; + + public EnableQuerySqlLogInnerInterceptor(InnerInterceptor delegate) { + Assert.notNull(delegate, "委派类不能为空"); + this.delegate = delegate; + } + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); + String sql = boundSql.getSql(); + log.info("执行mapperId{},原始sql为{}", ms.getId(), sql); + delegate.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql); + log.info("执行mapperId{}, 修改sql为{}", ms.getId(), mpBs.sql()); + } +} diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/sql/handler/CreatorDataScopeSqlHandler.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/sql/handler/CreatorDataScopeSqlHandler.java index b837a064e5ee23ecd8dfd2109c1d4ebfbe106347..40c616e176f558130e2b2d0a53765f56e0c3d477 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/sql/handler/CreatorDataScopeSqlHandler.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/datascope/mp/sql/handler/CreatorDataScopeSqlHandler.java @@ -40,7 +40,7 @@ public class CreatorDataScopeSqlHandler implements SqlHandler{ List roleList = userService.findRolesByUserId(user.getId()); return StrUtil.isBlank(dataScopeProperties.getCreatorIdColumnName()) ||CollUtil.isEmpty(roleList) - || roleList.stream().anyMatch(item-> Objects.nonNull(item.getDataScope()) || DataScope.ALL.equals(item.getDataScope())) + || roleList.stream().anyMatch(item-> Objects.isNull(item.getDataScope()) || DataScope.ALL.equals(item.getDataScope())) ? DO_NOTHING: // 这里确保有配置权限范围控制的字段 // 1. 如果没有配置角色的情况默认采用只读全部的记录 diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/properties/DataScopeProperties.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/properties/DataScopeProperties.java index e76925576989708b1acf57cd38d5f70d043c18ab..7d72dfe65ab3566274d75df6838cc5df4a705875 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/properties/DataScopeProperties.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/properties/DataScopeProperties.java @@ -1,8 +1,8 @@ package com.central.common.properties; import cn.hutool.core.collection.CollUtil; +import com.google.common.collect.ImmutableSet; import lombok.Data; -import lombok.Getter; import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.Collections; @@ -15,20 +15,30 @@ import java.util.Set; * @author jarvis create by 2023/1/8 */ @ConfigurationProperties(prefix = "zlt.datascope") -@Getter +@Data public class DataScopeProperties { + private static final Set INGORE_SQL_ID = ImmutableSet + .of("com.central.user.mapper.findRolesByUserId" + , "com.central.user.mapper.SysUserMapper.selectList" + , "com.central.user.mapper.SysUserRoleMapper.findRolesByUserId" + , "com.central.user.mapper.SysRoleMenuMapper.findMenusByRoleIds"); /** * 是否开启权限控制 */ private Boolean enabled = Boolean.FALSE; + + /** + * 是否开启打印sql的修改情况 + */ + private Boolean enabledSqlDebug = Boolean.FALSE; /** * 在includeTables的匹配符中过滤某几个表不需要权限的,仅enabled=true */ - private Set ignoreTables = Collections.singleton("SYS*"); + private Set ignoreTables = Collections.emptySet(); /** * 指定某几条sql不执行权限控制, 仅enabled=true生效 */ - private Set ignoreSqls = Collections.emptySet(); + private Set ignoreSqls = INGORE_SQL_ID; /** * 指定某几个表接受权限控制,仅enabled=true,默认当开启时全部表 */ @@ -38,27 +48,12 @@ public class DataScopeProperties { * 指定需要的字段名 */ private String creatorIdColumnName; - - public void setEnabled(Boolean enabled) { - this.enabled = enabled; - } - - public void setIgnoreTables(Set ignoreTables) { - HashSet ignoreSet = new HashSet<>(); - CollUtil.addAll(ignoreSet, ignoreTables); - CollUtil.addAll(ignoreSet, this.ignoreTables); - this.ignoreTables = ignoreSet; - } - public void setIgnoreSqls(Set ignoreSqls) { - this.ignoreSqls = ignoreSqls; - } - - public void setIncludeTables(Set includeTables) { - this.includeTables = includeTables; - } - - public void setCreatorIdColumnName(String creatorIdColumnName) { - this.creatorIdColumnName = creatorIdColumnName; + Set ingoreSet = new HashSet<>(); + ingoreSet.addAll(INGORE_SQL_ID); + if(CollUtil.isNotEmpty(ignoreSqls)){ + ingoreSet.addAll(ignoreSqls); + } + this.ignoreSqls = ingoreSet; } } diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java index 090a5c2a12693168884be73d4c45a836c235c246..9f732b071a0dc844bcc40cf70fe00a097d5cbc8a 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.central.common.datascope.mp.interceptor.DataScopeInnerInterceptor; +import com.central.common.datascope.mp.interceptor.EnableQuerySqlLogInnerInterceptor; import com.central.common.datascope.mp.sql.handler.CreatorDataScopeSqlHandler; import com.central.common.datascope.mp.sql.handler.SqlHandler; import com.central.common.properties.DataScopeProperties; @@ -61,7 +62,9 @@ public class MybatisPlusAutoConfigure { mpInterceptor.addInnerInterceptor(tenantInterceptor); } if(dataScopeProperties.getEnabled()){ - mpInterceptor.addInnerInterceptor(new DataScopeInnerInterceptor(dataScopeProperties, sqlHandler)); + DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor(dataScopeProperties, sqlHandler); + mpInterceptor.addInnerInterceptor(Boolean.TRUE.equals(dataScopeProperties.getEnabledSqlDebug()) + ? new EnableQuerySqlLogInnerInterceptor(dataScopeInnerInterceptor): dataScopeInnerInterceptor); } mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return mpInterceptor;