Search in sources :

Example 1 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project JBM by numen06.

the class MybatisPlusConfig method paginationInterceptor.

/**
 * 分页拦截器
 *
 * @return
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    paginationInterceptor.setDialectType("mysql");
    List<ISqlParser> sqlParserList = new ArrayList<>();
    // TenantSqlParser tenantSqlParser = new TenantSqlParser();
    // tenantSqlParser.setTenantHandler(new TenantHandler() {
    // @Override
    // public Expression getTenantId() {
    // return new LongValue(1L);
    // }
    // 
    // @Override
    // public String getTenantIdColumn() {
    // return "tenant_id";
    // }
    // 
    // @Override
    // public boolean doTableFilter(String tableName) {
    // // 这里可以判断是否过滤表
    // /*
    // * if ("user".equals(tableName)) { return true; }
    // */
    // return false;
    // }
    // });
    paginationInterceptor.setSqlParserList(sqlParserList);
    // });
    return paginationInterceptor;
}
Also used : ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) ArrayList(java.util.ArrayList) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 2 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project kms by mahonelau.

the class MybatisPlusConfig method tenantConfig.

/**
 * 多租户的配置
 * @param paginationInterceptor
 */
private void tenantConfig(PaginationInterceptor paginationInterceptor) {
    /*
         * 【测试多租户】 SQL 解析处理拦截器<br>
         * 这里固定写成住户 1 实际情况你可以从cookie读取,因此数据看不到 【 麻花藤 】 这条记录( 注意观察 SQL )<br>
         */
    List<ISqlParser> sqlParserList = new ArrayList<>();
    TenantSqlParser tenantSqlParser = new JeecgTenantParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId(boolean select) {
            String tenant_id = oConvertUtils.getString(TenantContext.getTenant(), "0");
            return new LongValue(tenant_id);
        }

        @Override
        public String getTenantIdColumn() {
            return tenant_field;
        }

        @Override
        public boolean doTableFilter(String tableName) {
            // return excludeTable.contains(tableName);
            if (tenantTable.contains(tableName)) {
                return false;
            }
            return true;
        }

        private Expression in(String ids) {
            final InExpression inExpression = new InExpression();
            inExpression.setLeftExpression(new Column(getTenantIdColumn()));
            final ExpressionList itemsList = new ExpressionList();
            final List<Expression> inValues = new ArrayList<>(2);
            for (String id : ids.split(",")) {
                inValues.add(new LongValue(id));
            }
            itemsList.setExpressions(inValues);
            inExpression.setRightItemsList(itemsList);
            return inExpression;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {

        @Override
        public boolean doFilter(MetaObject metaObject) {
            String sql = (String) metaObject.getValue(PluginUtils.DELEGATE_BOUNDSQL_SQL);
            for (String tableName : tenantTable) {
                String sql_lowercase = sql.toLowerCase();
                if (sql_lowercase.indexOf(tableName.toLowerCase()) >= 0) {
                    for (String key : DDL_KEYWORD) {
                        if (sql_lowercase.indexOf(key) >= 0) {
                            return true;
                        }
                    }
                    return false;
                }
            }
            /*if ("mapper路径.方法名".equals(ms.getId())) {
                    //使用这种判断也可以避免走此过滤器
                    return true;
                }*/
            return true;
        }
    });
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) MetaObject(org.apache.ibatis.reflection.MetaObject) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) ArrayList(java.util.ArrayList) ISqlParserFilter(com.baomidou.mybatisplus.core.parser.ISqlParserFilter) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 3 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project kykms by mahonelau.

the class MybatisPlusConfig method tenantConfig.

/**
 * 多租户的配置
 * @param paginationInterceptor
 */
private void tenantConfig(PaginationInterceptor paginationInterceptor) {
    /*
         * 【测试多租户】 SQL 解析处理拦截器<br>
         * 这里固定写成住户 1 实际情况你可以从cookie读取,因此数据看不到 【 麻花藤 】 这条记录( 注意观察 SQL )<br>
         */
    List<ISqlParser> sqlParserList = new ArrayList<>();
    TenantSqlParser tenantSqlParser = new JeecgTenantParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId(boolean select) {
            String tenant_id = oConvertUtils.getString(TenantContext.getTenant(), "0");
            return new LongValue(tenant_id);
        }

        @Override
        public String getTenantIdColumn() {
            return tenant_field;
        }

        @Override
        public boolean doTableFilter(String tableName) {
            // return excludeTable.contains(tableName);
            if (tenantTable.contains(tableName)) {
                return false;
            }
            return true;
        }

        private Expression in(String ids) {
            final InExpression inExpression = new InExpression();
            inExpression.setLeftExpression(new Column(getTenantIdColumn()));
            final ExpressionList itemsList = new ExpressionList();
            final List<Expression> inValues = new ArrayList<>(2);
            for (String id : ids.split(",")) {
                inValues.add(new LongValue(id));
            }
            itemsList.setExpressions(inValues);
            inExpression.setRightItemsList(itemsList);
            return inExpression;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {

        @Override
        public boolean doFilter(MetaObject metaObject) {
            String sql = (String) metaObject.getValue(PluginUtils.DELEGATE_BOUNDSQL_SQL);
            for (String tableName : tenantTable) {
                String sql_lowercase = sql.toLowerCase();
                if (sql_lowercase.indexOf(tableName.toLowerCase()) >= 0) {
                    for (String key : DDL_KEYWORD) {
                        if (sql_lowercase.indexOf(key) >= 0) {
                            return true;
                        }
                    }
                    return false;
                }
            }
            /*if ("mapper路径.方法名".equals(ms.getId())) {
                    //使用这种判断也可以避免走此过滤器
                    return true;
                }*/
            return true;
        }
    });
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) MetaObject(org.apache.ibatis.reflection.MetaObject) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) ArrayList(java.util.ArrayList) ISqlParserFilter(com.baomidou.mybatisplus.core.parser.ISqlParserFilter) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 4 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project tutorials-java by Artister.

the class MybatisPlusConfig method sqlExplainInterceptor.

@Bean
public SqlExplainInterceptor sqlExplainInterceptor() {
    SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
    List<ISqlParser> sqlParserList = new ArrayList<>();
    sqlParserList.add(new BlockAttackSqlParser());
    sqlExplainInterceptor.setSqlParserList(sqlParserList);
    return sqlExplainInterceptor;
}
Also used : ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) ArrayList(java.util.ArrayList) BlockAttackSqlParser(com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser) SqlExplainInterceptor(com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 5 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project dynamic_dataSource by tianliuzhen.

the class MybatisPlusConfig method paginationInterceptor.

/*  @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);



        List<ISqlParser> sqlParserList = new ArrayList<>();
        // 如果设置了全局逻辑删除、这里会失效,设置局部没关系已经测试
        // 攻击 SQL 阻断解析器、加入解析链
        sqlParserList.add(new BlockAttackSqlParser() {
            @Override
            public void processDelete(Delete delete) {
                // 如果你想自定义做点什么,可以重写父类方法像这样子
                if ("user_test".equals(delete.getTable().getName())) {
                    // 自定义跳过某个表,其他关联表可以调用 delete.getTables() 判断
                    log.info("跳过表:"+"user_test(这个表允许全部删除)");
                    return ;
                }
                super.processDelete(delete);
            }
        });
        paginationInterceptor.setSqlParserList(sqlParserList);

        return paginationInterceptor;
    }*/
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    /**
     * 1、【测试攻击 SQL】 阻断解析器、加入解析链
     *     自定义设置参考上面注释
     */
    List<ISqlParser> sqlParserList = new ArrayList<>();
    sqlParserList.add(new BlockAttackSqlParser());
    /**
     * 2、【测试多租户】 SQL 解析处理拦截器<br>
     * 传入的值一般都是配置文件 静态变量或者session中取出
     * 意思就是在你的所有的sql 加一个条件 即是 where  AND user.manager_id = 0
     */
    TenantSqlParser tenantSqlParser = new TenantSqlParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId(boolean where) {
            // 此判断用于支持返回多个租户 ID 场景,具体使用查看示例工程
            return new LongValue(0L);
        }

        @Override
        public String getTenantIdColumn() {
            // 多租户自定义字段
            return "manager_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            /*
            if ("user".equals(tableName)) {
                return true;
            }*/
            return false;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {

        @Override
        public boolean doFilter(MetaObject metaObject) {
            MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
            // 意思允许 UserMapper下的这个 getAll 不用加  AND user.manager_id = 0
            if ("com.aaa.mybatisplus.mapper.UserMapper.getAll".equals(ms.getId())) {
                return true;
            }
            return false;
        }
    });
    return paginationInterceptor;
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) MetaObject(org.apache.ibatis.reflection.MetaObject) ArrayList(java.util.ArrayList) BlockAttackSqlParser(com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser) ISqlParserFilter(com.baomidou.mybatisplus.core.parser.ISqlParserFilter) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) LongValue(net.sf.jsqlparser.expression.LongValue) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Bean(org.springframework.context.annotation.Bean)

Aggregations

ISqlParser (com.baomidou.mybatisplus.core.parser.ISqlParser)9 ArrayList (java.util.ArrayList)9 TenantHandler (com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler)6 TenantSqlParser (com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser)6 Expression (net.sf.jsqlparser.expression.Expression)6 LongValue (net.sf.jsqlparser.expression.LongValue)6 Bean (org.springframework.context.annotation.Bean)6 PaginationInterceptor (com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor)5 ISqlParserFilter (com.baomidou.mybatisplus.core.parser.ISqlParserFilter)4 MetaObject (org.apache.ibatis.reflection.MetaObject)4 BlockAttackSqlParser (com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser)3 List (java.util.List)3 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2 Column (net.sf.jsqlparser.schema.Column)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 SqlExplainInterceptor (com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor)1 CustomTenantSqlParser (com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser)1