mybatisplus3.5.1 租户插件过滤某个方法

本文介绍了如何在MybatisPlus的配置中使用自定义Interceptor,着重于TenantLineInnerInterceptor,它通过获取HTTP请求头中的token从Redis中获取租户ID,并忽略了特定表的操作。同时提到了如何配置缓存和表名判断逻辑。

 重写

TenantLineInnerInterceptor#beforeQuery
@Configuration
@MapperScan("com.fs.zhnm.edge.data.center.accountauth.dao")
public class MybatisPlusConfig {


    @Autowired
    private RedisFeignAPI redisFeignAPI;

    public static final String TENANT_CASE_KEY = "TENANT_CASE_KEY";

    /**
     * 忽略添加租户ID的表
     */
    private static List<String> IGNORE_TABLE_NAMES = Lists.newArrayList(
            "account_employee_relation","account_org_relation","account_role_relation","application_info","default_application"
            ,"operate_auth","operate_auth_app","operation_account_info","role_operate_auth_relation","tenement_application_relation","tenement_info"
    );

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new TenantLineHandler()
        {
            @Override
            public Expression getTenantId() {
                HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
                String token = request.getHeader("token");
                RetResult<Object> value = redisFeignAPI.getValue(token);
                AccountInfoVo accountInfoVo = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(new Gson().toJson(value.getData()), AccountInfoVo.class);
                if (null != accountInfoVo) {
                    Long tenementId = accountInfoVo.getTenementId();
                    if (null == tenementId) {
                        return new NullValue();
                    }
                    //租户Id,可以从缓存或者cookie,token等中获取
                    return new LongValue(accountInfoVo.getTenementId());
                }
                return new NullValue();
            }

            /**
             * 获取租户字段名(数据库的租户ID字段名)
             *
             * @return
             */
            @Override
            public String getTenantIdColumn() {
                return "tenement_id";
            }

            /**
             * 根据表名判断是否忽略拼接多租户条件
             *
             * @param tableName
             * @return
             */
            @Override
            public boolean ignoreTable(String tableName) {
                return IGNORE_TABLE_NAMES.contains(tableName);
            }
        }));
        PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        interceptor.addInnerInterceptor(innerInterceptor);
        return interceptor;
    }
}
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {

    public MyTenantLineInnerInterceptor() {
        super();
    }

    public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }

    private static List<String> IGNORE_STATEMENT_NAMES = Lists.newArrayList(
            "com.fs.zhnm.edge.data.center.accountauth.dao.AccountInfoDao.getLoginAccountRole"
    );

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        // 如果statementid存在,则忽略方法
        if (IGNORE_STATEMENT_NAMES.contains(ms.getId())) return;
        super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }
}

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值