mybatisplus添加數據權限過濾(自定義攔截器,sql攔截)


    import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
    import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
    import lombok.AllArgsConstructor;
    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 org.springframework.stereotype.Component;

    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.util.Properties;

    @Slf4j
    @AllArgsConstructor
    @Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
    @Component
    public class DataScopeInterceptor  extends AbstractSqlParserHandler implements Interceptor {
        private DataSource dataSource;

        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            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");
            // 執行的SQL語句
            String originalSql = boundSql.getSql();
            // SQL語句的參數
            Object parameterObject = boundSql.getParameterObject();

                originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + 1 + " in (" + 2 + ")";
                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;
    //    }
    }

以下代碼添加至mybatisplusconfig

    /**
     * 數據權限插件
     *
     * @return DataScopeInterceptor
     */
    @Bean
    @ConditionalOnMissingBean
    public DataScopeInterceptor dataScopeInterceptor(DataSource dataSource) {
        return new DataScopeInterceptor(dataSource);
    }

 原文:https://blog.rain888.cn/archives/328.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM