使用Mybatis-plus多租戶解決越權問題


使用Mybatis-plus多租戶解決越權問題

背景

權限校驗完畢后,還需要解決水平越權問題,使用MyBatis-plus中的多租戶攔截器可以輕松解決。

文檔地址:https://mp.baomidou.com/guide/interceptor-tenant-line.html#tenantlineinnerinterceptor

代碼

@Configuration
public class MybatisPlusPageConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {

        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        mybatisPlusInterceptor.addInnerInterceptor(new TenantLineInnerInterceptor(this.tenantLineHandler()));
        return mybatisPlusInterceptor;
    }

    /**
     * 多租戶的形式解決越權問題
     * @return
     */
    private TenantLineHandler tenantLineHandler() {

        return new TenantLineHandler() {
            /**
             * 租戶id
             * @return
             */
            @Override
            public Expression getTenantId() {
                return new LongValue(LoginInterceptor.threadLocal.get().getId());
            }

            /**
             * 查詢的列,指數據庫中的列
             * @return
             */
            @Override
            public String getTenantIdColumn() {
                return "user_id";
            }

            /**
             * 忽略的表名
             * @param tableName
             * @return
             */
            @Override
            public boolean ignoreTable(String tableName) {
                if (tableName.equalsIgnoreCase("address")) {
                    return false;
                }
                return true;
            }
        };
    }

}


免責聲明!

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



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