MybatisPlus實(shí)現(xiàn)數(shù)據(jù)權(quán)限隔離的示例詳解
引言
Mybatis Plus對(duì)Mybatis做了無(wú)侵入的增強(qiáng),非常的好用,今天就給大家介紹它的其中一個(gè)實(shí)用功能:數(shù)據(jù)權(quán)限插件。
依賴
首先導(dǎo)入Mybatis Plus的maven依賴,我使用的是3.5.3.2版本。
<properties> <mybatis-plus.version>3.5.3.2</mybatis-plus.version> </properties> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency>
數(shù)據(jù)權(quán)限攔截器
寫一個(gè)自定義的權(quán)限注解,該注解用來(lái)標(biāo)注被攔截方法,注解上可以配置數(shù)據(jù)權(quán)限的表別名和表字段,它們會(huì)在拼接sql的時(shí)候用到。
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyDataScope { /** * 表別名設(shè)置 */ String alias() default ""; /** * 數(shù)據(jù)權(quán)限表字段名 */ String dataId() default ""; }
接下來(lái)就是寫最核心的攔截器的處理邏輯了。創(chuàng)建一個(gè)接口實(shí)現(xiàn)類,實(shí)現(xiàn)Mybatis Plus的DataPermissionHandler接口。DataPermissionHandler的接口方法getSqlSegment有兩個(gè)參數(shù)。
- Expression where。where參數(shù)是mapper接口在xml中定義的sql的where條件表達(dá)式,在攔截處理器中我們可以給where條件表達(dá)式添加一些 and 或 or 的條件。
- String mappedStatementId。mappedStatementId參數(shù)是mapper接口方法的全限定名,通過(guò)它我們可以得到mapper接口的Class類名以及接口方法名。
DataPermissionHandler的接口方法getSqlSegment會(huì)返回一個(gè)Expression類型的結(jié)果,即通過(guò)攔截器方法我們將原始的where條件表達(dá)式做了修改之后返回給Mybatis Plus并在代碼運(yùn)行時(shí)生效。
在攔截器方法中還使用到了一開(kāi)始我們自定義的MyDataScope注解,沒(méi)有被MyDataScope注解標(biāo)注過(guò)的mapper方法我們直接返回原始的where條件表達(dá)式即可。
import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler; import com.itguoguo.annotation.MyDataScope; import com.itguoguo.system.api.model.LoginUser; import lombok.extern.slf4j.Slf4j; import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import java.lang.reflect.Method; import java.util.Objects; import static com.itguoguo.utils.LoginUserUtils.getLoginUser; @Slf4j public class MyDataScopeHandler implements DataPermissionHandler { /** * 獲取數(shù)據(jù)權(quán)限 SQL 片段表達(dá)式 * @param where 待執(zhí)行 SQL Where 條件表達(dá)式 * @param mappedStatementId Mybatis MappedStatement Id 根據(jù)該參數(shù)可以判斷具體執(zhí)行方法 * @return 數(shù)據(jù)權(quán)限 SQL 片段表達(dá)式 */ @Override public Expression getSqlSegment(Expression where, String mappedStatementId) { try { String className = mappedStatementId.substring(0, mappedStatementId.lastIndexOf(".")); String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(".") + 1); Method[] methods = Class.forName(className).getMethods(); for (Method m : methods) { if (StrUtil.isBlank(m.getName()) || !m.getName().equals(methodName)) { continue; } MyDataScope annotation = m.getAnnotation(MyDataScope.class); if (Objects.isNull(annotation)) { return where; } String sqlSegment = getSqlSegment(annotation); return StrUtil.isBlank(sqlSegment) ? where : getExpression(where, sqlSegment); } } catch (ClassNotFoundException e) { log.error(e.getMessage(), e); } return null; } /** * 拼接需要在業(yè)務(wù) SQL 中額外追加的數(shù)據(jù)權(quán)限 SQL * @param annotation * @return 數(shù)據(jù)權(quán)限 SQL */ private String getSqlSegment(MyDataScope annotation) { LoginUser loginUser = getLoginUser(); String userType = loginUser.getSysUser().getUserType(); Long userId = loginUser.getSysUser().getUserId(); String sqlSegment = ""; if (StrUtil.isBlank(userType)) { return sqlSegment; } if ("0".equals(userType)) { return sqlSegment; } else { sqlSegment = StrUtil.format(" {}.{} IN (SELECT project_id FROM sys_user where user_id = '{}') ", annotation.alias(), annotation.dataId(), userId); } return sqlSegment; } /** * 將數(shù)據(jù)權(quán)限 SQL 語(yǔ)句追加到數(shù)據(jù)權(quán)限 SQL 片段表達(dá)式里 * @param where 待執(zhí)行 SQL Where 條件表達(dá)式 * @param sqlSegment 數(shù)據(jù)權(quán)限 SQL 片段 * @return */ private Expression getExpression(Expression where, String sqlSegment) { try { Expression sqlSegmentExpression = CCJSqlParserUtil.parseCondExpression(sqlSegment); return (null != where) ? new AndExpression(where, sqlSegmentExpression) : sqlSegmentExpression; } catch (JSQLParserException e) { log.error(e.getMessage(), e); } return null; } }
攔截器配置
配置Mybatis Plus攔截器,數(shù)據(jù)權(quán)限handler作為參數(shù)傳給攔截器構(gòu)造方法。
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new DataPermissionInterceptor(new MyDataScopeHandler())); return interceptor; } }
使用
使用時(shí),在mapper接口的方法上標(biāo)注MyDataScope注解,給注解標(biāo)上表別名和表字段。
public interface MyMapper { @MyDataScope(alias = "a", dataId = "id") List findList(); }
到此這篇關(guān)于MybatisPlus實(shí)現(xiàn)數(shù)據(jù)權(quán)限隔離的示例詳解的文章就介紹到這了,更多相關(guān)MybatisPlus數(shù)據(jù)權(quán)限隔離內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)權(quán)限隔離方式
- MyBatis-Plus數(shù)據(jù)權(quán)限插件的簡(jiǎn)單使用
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的方法
- Mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限詳解
- Mybatis-plus通過(guò)添加攔截器實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)權(quán)限
- mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限項(xiàng)目實(shí)踐
- mybatis-plus數(shù)據(jù)權(quán)限實(shí)現(xiàn)代碼
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的示例
- Mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限的示例代碼
- Mybatis自定義攔截器實(shí)現(xiàn)權(quán)限功能
相關(guān)文章
Spring Boot 中的 @ConditionalOnBean 注解場(chǎng)景分析
本文詳細(xì)介紹了Spring Boot中的@ConditionalOnBean注解的使用場(chǎng)景、原理和基本用法,通過(guò)多個(gè)示例,展示了如何使用該注解根據(jù)Bean是否存在來(lái)動(dòng)態(tài)地注冊(cè)或跳過(guò)特定的Bean,感興趣的朋友一起看看吧2025-03-03淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn)
這篇文章主要介紹了淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06SpringBoot集成E-mail發(fā)送各種類型郵件
這篇文章主要為大家詳細(xì)介紹了SpringBoot集成E-mail發(fā)送各種類型郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Java操作Mongodb數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)的增刪查改功能示例
這篇文章主要介紹了Java操作Mongodb數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)的增刪查改功能,結(jié)合完整實(shí)例形式分析了java針對(duì)MongoDB數(shù)據(jù)庫(kù)的連接、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Kafka單節(jié)點(diǎn)偽分布式集群搭建實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Kafka單節(jié)點(diǎn)偽分布式集群搭建實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11