Search in sources :

Example 6 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project ddf-common by dongfangding.

the class MyBatisConfig method paginationInterceptor.

/**
 * 分页与攻击 SQL 阻断解析器
 *
 * @return
 */
@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());
    paginationInterceptor.setSqlParserList(sqlParserList);
    return paginationInterceptor;
}
Also used : ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) ArrayList(java.util.ArrayList) BlockAttackSqlParser(com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 7 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project jshERP by jishenghua.

the class TenantConfig method paginationInterceptor.

@Bean
public PaginationInterceptor paginationInterceptor(HttpServletRequest request) {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    List<ISqlParser> sqlParserList = new ArrayList<>();
    TenantSqlParser tenantSqlParser = new TenantSqlParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId() {
            String token = request.getHeader("X-Access-Token");
            Long tenantId = Tools.getTenantIdByToken(token);
            if (tenantId != 0L) {
                return new LongValue(tenantId);
            } else {
                // 超管
                return null;
            }
        }

        @Override
        public String getTenantIdColumn() {
            return "tenant_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            // 获取开启状态
            Boolean res = true;
            String token = request.getHeader("X-Access-Token");
            Long tenantId = Tools.getTenantIdByToken(token);
            if (tenantId != 0L) {
                // 这里可以判断是否过滤表
                if ("jsh_material_property".equals(tableName) || "jsh_sequence".equals(tableName) || "jsh_user_business".equals(tableName) || "jsh_function".equals(tableName) || "jsh_platform_config".equals(tableName) || "jsh_tenant".equals(tableName)) {
                    res = true;
                } else {
                    res = false;
                }
            }
            return res;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {

        @Override
        public boolean doFilter(MetaObject metaObject) {
            MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
            // 过滤自定义查询此时无租户信息约束出现
            if ("com.jsh.erp.datasource.mappers.UserMapperEx.getUserListByUserNameOrLoginName".equals(ms.getId())) {
                return true;
            } else if ("com.jsh.erp.datasource.mappers.RoleMapperEx.getRoleWithoutTenant".equals(ms.getId())) {
                return true;
            } else if ("com.jsh.erp.datasource.mappers.LogMapperEx.insertLogWithUserId".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) 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)

Example 8 with ISqlParser

use of com.baomidou.mybatisplus.core.parser.ISqlParser in project springboot-learning by lyb-geek.

the class MyBatisPlusTenantConfig method tenantHandler.

/**
 * 租户处理
 * @param paginationInterceptor
 */
private void tenantHandler(PaginationInterceptor paginationInterceptor) {
    // 创建SQL解析器集合
    List<ISqlParser> sqlParserList = new ArrayList<>();
    // 创建租户SQL解析器
    TenantSqlParser tenantSqlParser = new CustomTenantSqlParser();
    // 设置租户处理器
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId() {
            return new LongValue(100);
        }

        @Override
        public String getTenantIdColumn() {
            // 对应数据库租户ID的列名
            return "tenant_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            List<String> skipFillTenantIdTables = myBatisPlusTenantProperties.getSkipFillTenantIdTables();
            return skipFillTenantIdTables.contains(tableName);
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) CustomTenantSqlParser(com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser) ArrayList(java.util.ArrayList) LongValue(net.sf.jsqlparser.expression.LongValue) ArrayList(java.util.ArrayList) List(java.util.List) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) CustomTenantSqlParser(com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser)

Example 9 with ISqlParser

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

the class MybatisPlusConfig method paginationInterceptor.

/**
 * 多租户属于 SQL 解析部分,依赖 MP 分页插件
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    /*
         * 【测试多租户】 SQL 解析处理拦截器<br>
         * 这里固定写成住户 1 实际情况你可以从cookie读取,因此数据看不到 【 麻花藤 】 这条记录( 注意观察 SQL )<br>
         */
    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;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    // });
    return paginationInterceptor;
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) LongValue(net.sf.jsqlparser.expression.LongValue) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) 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