From c03076e711b8215d2d7cb190fa8b6c813f0ffa2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=83?= Date: Sun, 29 Nov 2020 13:54:55 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20#2?= =?UTF-8?q?005=20=E5=8D=87=E7=BA=A7=20mybatis-plus=203.4.1=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8F=92=E4=BB=B6=E4=B8=BB=E4=BD=93=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=20=20e5dd87ab6691d0fcd1efeaf2708ff0130d6a6c9?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datascope/DataScopeInnerInterceptor.java | 79 ++++++++++ .../data/datascope/DataScopeInterceptor.java | 138 ------------------ .../mybatis/MybatisPlusConfiguration.java | 27 ++-- 3 files changed, 89 insertions(+), 155 deletions(-) create mode 100644 pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java delete mode 100644 pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java new file mode 100644 index 000000000..abeeadd52 --- /dev/null +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java @@ -0,0 +1,79 @@ +package com.pig4cloud.pigx.common.data.datascope; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.CollectionUtil; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import lombok.Setter; +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.util.List; +import java.util.Map; + +/** + * @author lengleng + * @date 2020/11/29 + */ +public class DataScopeInnerInterceptor implements InnerInterceptor { + + @Setter + private DataScopeHandle dataScopeHandle; + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, + ResultHandler resultHandler, BoundSql boundSql) { + PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); + + String originalSql = boundSql.getSql(); + Object parameterObject = boundSql.getParameterObject(); + + // 查找参数中包含DataScope类型的参数 + DataScope dataScope = findDataScopeObject(parameterObject); + if (dataScope == null) { + return; + } + + String scopeName = dataScope.getScopeName(); + List deptIds = dataScope.getDeptIds(); + // 优先获取赋值数据 + if (CollUtil.isEmpty(deptIds) && dataScopeHandle.calcScope(deptIds)) { + return; + } + + if (deptIds.isEmpty()) { + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2", + dataScope.getFunc().getType(), originalSql); + } + else { + String join = CollectionUtil.join(deptIds, ","); + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)", + dataScope.getFunc().getType(), originalSql, scopeName, join); + } + + mpBs.sql(originalSql); + } + + /** + * 查找参数是否包括DataScope对象 + * @param parameterObj 参数列表 + * @return DataScope + */ + private DataScope findDataScopeObject(Object parameterObj) { + if (parameterObj instanceof DataScope) { + return (DataScope) parameterObj; + } + else if (parameterObj instanceof Map) { + for (Object val : ((Map) parameterObj).values()) { + if (val instanceof DataScope) { + return (DataScope) val; + } + } + } + return null; + } + +} diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java deleted file mode 100644 index e0ca09182..000000000 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.common.data.datascope; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.collection.CollectionUtil; -import com.baomidou.mybatisplus.core.toolkit.PluginUtils; -import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler; -import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; -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.plugin.*; -import org.apache.ibatis.reflection.MetaObject; -import org.apache.ibatis.reflection.SystemMetaObject; - -import java.sql.Connection; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -/** - * @author lengleng - * @date 2020/5/27 - *

- * mybatis 数据权限拦截器 - */ -@Slf4j -@RequiredArgsConstructor -@Intercepts({ - @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) }) -public class DataScopeInterceptor extends AbstractSqlParserHandler implements Interceptor { - - private final DataScopeHandle dataScopeHandle; - - @Override - @SneakyThrows - public Object intercept(Invocation invocation) { - StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget()); - MetaObject metaObject = SystemMetaObject.forObject(statementHandler); - this.sqlParser(metaObject); - // 先判断是不是SELECT操作 - MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); - if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) { - return invocation.proceed(); - } - - BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql"); - String originalSql = boundSql.getSql(); - Object parameterObject = boundSql.getParameterObject(); - - // 查找参数中包含DataScope类型的参数 - DataScope dataScope = findDataScopeObject(parameterObject); - if (dataScope == null) { - return invocation.proceed(); - } - - String scopeName = dataScope.getScopeName(); - List deptIds = dataScope.getDeptIds(); - // 优先获取赋值数据 - if (CollUtil.isEmpty(deptIds) && dataScopeHandle.calcScope(deptIds)) { - return invocation.proceed(); - } - - if (deptIds.isEmpty()) { - originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2", - dataScope.getFunc().getType(), originalSql); - } - else { - String join = CollectionUtil.join(deptIds, ","); - originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)", - dataScope.getFunc().getType(), originalSql, scopeName, join); - } - - metaObject.setValue("delegate.boundSql.sql", originalSql); - return invocation.proceed(); - } - - /** - * 生成拦截对象的代理 - * @param target 目标对象 - * @return 代理对象 - */ - @Override - public Object plugin(Object target) { - if (target instanceof StatementHandler) { - return Plugin.wrap(target, this); - } - return target; - } - - /** - * mybatis配置的属性 - * @param properties mybatis配置的属性 - */ - @Override - public void setProperties(Properties properties) { - - } - - /** - * 查找参数是否包括DataScope对象 - * @param parameterObj 参数列表 - * @return DataScope - */ - private DataScope findDataScopeObject(Object parameterObj) { - if (parameterObj instanceof DataScope) { - return (DataScope) parameterObj; - } - else if (parameterObj instanceof Map) { - for (Object val : ((Map) parameterObj).values()) { - if (val instanceof DataScope) { - return (DataScope) val; - } - } - } - return null; - } - -} diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/mybatis/MybatisPlusConfiguration.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/mybatis/MybatisPlusConfiguration.java index 5e1475681..119be18a4 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/mybatis/MybatisPlusConfiguration.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/mybatis/MybatisPlusConfiguration.java @@ -22,7 +22,7 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.pig4cloud.pigx.common.data.datascope.DataScopeHandle; -import com.pig4cloud.pigx.common.data.datascope.DataScopeInterceptor; +import com.pig4cloud.pigx.common.data.datascope.DataScopeInnerInterceptor; import com.pig4cloud.pigx.common.data.datascope.DataScopeSqlInjector; import com.pig4cloud.pigx.common.data.datascope.PigxDefaultDatascopeHandle; import com.pig4cloud.pigx.common.data.resolver.SqlFilterArgumentResolver; @@ -58,19 +58,18 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer { } /** - * 数据权限插件 优先 mybatis-plus 的拦截器执行 - * @return DataScopeInterceptor + * pigx 默认数据权限处理器 + * @return PigxDefaultDatascopeHandle */ @Bean @ConditionalOnMissingBean - @ConditionalOnBean(DataScopeHandle.class) - public DataScopeInterceptor dataScopeInterceptor() { - return new DataScopeInterceptor(dataScopeHandle()); + public DataScopeHandle dataScopeHandle() { + return new PigxDefaultDatascopeHandle(); } /** * mybatis plus 拦截器配置 - * @return + * @return PigxDefaultDatascopeHandle */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { @@ -79,6 +78,10 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer { TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(); tenantLineInnerInterceptor.setTenantLineHandler(pigxTenantHandler()); interceptor.addInnerInterceptor(tenantLineInnerInterceptor); + // 数据权限 + DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor(); + dataScopeInnerInterceptor.setDataScopeHandle(dataScopeHandle()); + interceptor.addInnerInterceptor(dataScopeInnerInterceptor); // 分页支持 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; @@ -94,16 +97,6 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer { return new PigxTenantHandler(); } - /** - * pigx 默认数据权限处理器 - * @return - */ - @Bean - @ConditionalOnMissingBean - public DataScopeHandle dataScopeHandle() { - return new PigxDefaultDatascopeHandle(); - } - /** * 扩展 mybatis-plus baseMapper 支持数据权限 * @return