mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限項(xiàng)目實(shí)踐
前端的菜單和按鈕權(quán)限都可以通過(guò)配置來(lái)實(shí)現(xiàn),但很多時(shí)候,后臺(tái)查詢數(shù)據(jù)庫(kù)數(shù)據(jù)的權(quán)限需要通過(guò)手動(dòng)添加SQL來(lái)實(shí)現(xiàn)。比如員工打卡記錄表,有id,name,dpt_id,company_id等字段,后兩個(gè)表示部門(mén)ID和分公司ID。查看員工打卡記錄SQL為:select id,name,dpt_id,company_id from t_record
當(dāng)一個(gè)總部賬號(hào)可以查看全部數(shù)據(jù)此時(shí),sql無(wú)需改變。因?yàn)樗梢钥吹饺繑?shù)據(jù)。當(dāng)一個(gè)部門(mén)管理員權(quán)限員工查看全部數(shù)據(jù)時(shí),sql需要在末屬添加 where dpt_id = #{dpt_id}
如果每個(gè)功能模塊都需要手動(dòng)寫(xiě)代碼去拿到當(dāng)前登陸用戶的所屬部門(mén),然后手動(dòng)添加where條件,就顯得非常的繁瑣。因此,可以通過(guò)mybatis的攔截器拿到查詢sql語(yǔ)句,再自動(dòng)改寫(xiě)sql。
mybatis 攔截器
MyBatis 允許你在映射語(yǔ)句執(zhí)行過(guò)程中的某一點(diǎn)進(jìn)行攔截調(diào)用。默認(rèn)情況下,MyBatis 允許使用插件來(lái)攔截的方法調(diào)用包括:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
這些類中方法的細(xì)節(jié)可以通過(guò)查看每個(gè)方法的簽名來(lái)發(fā)現(xiàn),或者直接查看 MyBatis 發(fā)行包中的源代碼。 如果你想做的不僅僅是監(jiān)控方法的調(diào)用,那么你最好相當(dāng)了解要重寫(xiě)的方法的行為。 因?yàn)樵谠噲D修改或重寫(xiě)已有方法的行為時(shí),很可能會(huì)破壞 MyBatis 的核心模塊。 這些都是更底層的類和方法,所以使用插件的時(shí)候要特別當(dāng)心。
通過(guò) MyBatis 提供的強(qiáng)大機(jī)制,使用插件是非常簡(jiǎn)單的,只需實(shí)現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即可。
分頁(yè)插件pagehelper就是一個(gè)典型的通過(guò)攔截器去改寫(xiě)SQL的。

可以看到它通過(guò)注解 @Intercepts 和簽名 @Signature 來(lái)實(shí)現(xiàn),攔截Executor執(zhí)行器,攔截所有的query查詢類方法。我們可以據(jù)此也實(shí)現(xiàn)自己的攔截器。
import com.skycomm.common.util.user.Cpip2UserDeptVo;
import com.skycomm.common.util.user.Cpip2UserDeptVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
@Component
@Intercepts({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
})
@Slf4j
public class MySqlInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
Object parameter = invocation.getArgs()[1];
BoundSql boundSql = statement.getBoundSql(parameter);
String originalSql = boundSql.getSql();
Object parameterObject = boundSql.getParameterObject();
SqlLimit sqlLimit = isLimit(statement);
if (sqlLimit == null) {
return invocation.proceed();
}
RequestAttributes req = RequestContextHolder.getRequestAttributes();
if (req == null) {
return invocation.proceed();
}
//處理request
HttpServletRequest request = ((ServletRequestAttributes) req).getRequest();
Cpip2UserDeptVo userVo = Cpip2UserDeptVoUtil.getUserDeptInfo(request);
String depId = userVo.getDeptId();
String sql = addTenantCondition(originalSql, depId, sqlLimit.alis());
log.info("原SQL:{}, 數(shù)據(jù)權(quán)限替換后的SQL:{}", originalSql, sql);
BoundSql newBoundSql = new BoundSql(statement.getConfiguration(), sql, boundSql.getParameterMappings(), parameterObject);
MappedStatement newStatement = copyFromMappedStatement(statement, new BoundSqlSqlSource(newBoundSql));
invocation.getArgs()[0] = newStatement;
return invocation.proceed();
}
/**
* 重新拼接SQL
*/
private String addTenantCondition(String originalSql, String depId, String alias) {
String field = "dpt_id";
if(StringUtils.isNoneBlank(alias)){
field = alias + "." + field;
}
StringBuilder sb = new StringBuilder(originalSql);
int index = sb.indexOf("where");
if (index < 0) {
sb.append(" where ") .append(field).append(" = ").append(depId);
} else {
sb.insert(index + 5, " " + field +" = " + depId + " and ");
}
return sb.toString();
}
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.cache(ms.getCache());
builder.useCache(ms.isUseCache());
return builder.build();
}
/**
* 通過(guò)注解判斷是否需要限制數(shù)據(jù)
* @return
*/
private SqlLimit isLimit(MappedStatement mappedStatement) {
SqlLimit sqlLimit = null;
try {
String id = mappedStatement.getId();
String className = id.substring(0, id.lastIndexOf("."));
String methodName = id.substring(id.lastIndexOf(".") + 1, id.length());
final Class<?> cls = Class.forName(className);
final Method[] method = cls.getMethods();
for (Method me : method) {
if (me.getName().equals(methodName) && me.isAnnotationPresent(SqlLimit.class)) {
sqlLimit = me.getAnnotation(SqlLimit.class);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sqlLimit;
}
public static class BoundSqlSqlSource implements SqlSource {
private final BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
@Override
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
}順便加了個(gè)注解 @SqlLimit,在mapper方法上加了此注解才進(jìn)行數(shù)據(jù)權(quán)限過(guò)濾。同時(shí)注解有兩個(gè)屬性,
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SqlLimit {
/**
* sql表別名
* @return
*/
String alis() default "";
/**
* 通過(guò)此列名進(jìn)行限制
* @return
*/
String columnName() default "";
}columnName表示通過(guò)此列名進(jìn)行限制,一般來(lái)說(shuō)一個(gè)系統(tǒng),各表當(dāng)中的此列是統(tǒng)一的,可以忽略。
alis用于標(biāo)注sql表別名,如 針對(duì)sql select * from tablea as a left join tableb as b on a.id = b.id 進(jìn)行改寫(xiě),如果不知道表別名,會(huì)直接在后面拼接 where dpt_id = #{dptId},那此SQL就會(huì)錯(cuò)誤的,通過(guò)別名 @SqlLimit(alis = "a") 就可以知道需要拼接的是 where a.dpt_id = #{dptId}
執(zhí)行結(jié)果
原SQL:select * from person, 數(shù)據(jù)權(quán)限替換后的SQL:select * from person where dpt_id = 234
原SQL:select * from person where id > 1, 數(shù)據(jù)權(quán)限替換后的SQL:select * from person where dpt_id = 234 and id > 1
但是在使用PageHelper進(jìn)行分頁(yè)的時(shí)候還是有問(wèn)題。

可以看到先執(zhí)行了_COUNT方法也就是PageHelper,再執(zhí)行了自定義的攔截器。
在我們的業(yè)務(wù)方法中注入SqlSessionFactory
@Autowired@Lazyprivate List<SqlSessionFactory> sqlSessionFactoryList;@Autowired @Lazy private List<SqlSessionFactory> sqlSessionFactoryList;
PageInterceptor為1,自定義攔截器為0,跟order相反,PageInterceptor優(yōu)先級(jí)更高,所以越先執(zhí)行。
mybatis攔截器優(yōu)先級(jí)
@Order
通過(guò)@Order控制PageInterceptor和MySqlInterceptor可行嗎?

將MySqlInterceptor的加載優(yōu)先級(jí)調(diào)到最高,但測(cè)試證明依然不行。
定義3個(gè)類
@Component
@Order(2)
public class OrderTest1 {
@PostConstruct
public void init(){
System.out.println(" 00000 init");
}
}@Component
@Order(1)
public class OrderTest2 {
@PostConstruct
public void init(){
System.out.println(" 00001 init");
}
}@Component
@Order(0)
public class OrderTest3 {
@PostConstruct
public void init(){
System.out.println(" 00002 init");
}
}OrderTest1,OrderTest2,OrderTest3的優(yōu)先級(jí)從低到高。順序預(yù)期的執(zhí)行順序應(yīng)該是相反的:
00002 init
00001 init
00000 init
但事實(shí)上執(zhí)行的順序是
00000 init
00001 init
00002 init
@Order 不控制實(shí)例化順序,只控制執(zhí)行順序。@Order 只跟特定一些注解生效 如:@Compent @Service @Aspect … 不生效的如: @WebFilter
所以這里達(dá)不到預(yù)期效果。
@Priority 類似,同樣不行。
@DependsOn
使用此注解將當(dāng)前類將在依賴類實(shí)例化之后再執(zhí)行實(shí)例化。
在MySqlInterceptor上標(biāo)記@DependsOn("queryInterceptor")

啟動(dòng)報(bào)錯(cuò),這個(gè)時(shí)候queryInterceptor還沒(méi)有實(shí)例化對(duì)象。
@PostConstruct
@PostConstruct修飾的方法會(huì)在服務(wù)器加載Servlet的時(shí)候運(yùn)行,并且只會(huì)被服務(wù)器執(zhí)行一次。在同一個(gè)類里,執(zhí)行順序?yàn)轫樞蛉缦?Constructor > @Autowired > @PostConstruct。
但它也不能保證不同類的執(zhí)行順序。
PageHelper的springboot start也是通過(guò)這個(gè)來(lái)初始化攔截器的。

ApplicationRunner
在當(dāng)前springboot容器加載完成后執(zhí)行,那么這個(gè)時(shí)候pagehelper的攔截器已經(jīng)加入,在這個(gè)時(shí)候加入自定義攔截器,就能達(dá)到我們想要的效果。
仿照PageHelper來(lái)寫(xiě)
@Component
public class InterceptRunner implements ApplicationRunner {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@Override
public void run(ApplicationArguments args) throws Exception {
MySqlInterceptor mybatisInterceptor = new MySqlInterceptor();
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
configuration.addInterceptor(mybatisInterceptor);
}
}
}再執(zhí)行,可以看到自定義攔截器在攔截器鏈當(dāng)中下標(biāo)變?yōu)榱?(優(yōu)先級(jí)與order剛好相反)

后臺(tái)打印結(jié)果,達(dá)到了預(yù)期效果。

到此這篇關(guān)于mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)mybatis 數(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)單使用
- MybatisPlus實(shí)現(xiàn)數(shù)據(jù)權(quá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-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)文章
JAVA String.valueOf()方法的用法說(shuō)明
這篇文章主要介紹了JAVA String.valueOf()方法的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
MyBatis-Plus里面的增刪改查詳解(化繁為簡(jiǎn))
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus里面的增刪改查的相關(guān)資料,Mybatis-Plus是一個(gè)基于Mybatis的增強(qiáng)工具,可以簡(jiǎn)化Mybatis的開(kāi)發(fā),提高開(kāi)發(fā)效率,需要的朋友可以參考下2023-07-07
最有價(jià)值的50道java面試題 適用于準(zhǔn)入職Java程序員
這篇文章主要為大家分享了最有價(jià)值的50道java面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對(duì)hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下2016-05-05
Java線程協(xié)調(diào)運(yùn)行操作實(shí)例詳解
這篇文章主要介紹了Java線程協(xié)調(diào)運(yùn)行操作,結(jié)合具體實(shí)例形式詳細(xì)分析了Java線程協(xié)調(diào)運(yùn)行原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
Java?根據(jù)XPATH批量替換XML節(jié)點(diǎn)中的值
這篇文章主要介紹了Java根據(jù)XPATH批量替換XML節(jié)點(diǎn)中的值,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

