mybatis攔截器實現(xiàn)數(shù)據(jù)權限項目實踐
前端的菜單和按鈕權限都可以通過配置來實現(xiàn),但很多時候,后臺查詢數(shù)據(jù)庫數(shù)據(jù)的權限需要通過手動添加SQL來實現(xiàn)。比如員工打卡記錄表,有id,name,dpt_id,company_id等字段,后兩個表示部門ID和分公司ID。查看員工打卡記錄SQL為:select id,name,dpt_id,company_id from t_record
當一個總部賬號可以查看全部數(shù)據(jù)此時,sql無需改變。因為他可以看到全部數(shù)據(jù)。當一個部門管理員權限員工查看全部數(shù)據(jù)時,sql需要在末屬添加 where dpt_id = #{dpt_id}
如果每個功能模塊都需要手動寫代碼去拿到當前登陸用戶的所屬部門,然后手動添加where條件,就顯得非常的繁瑣。因此,可以通過mybatis的攔截器拿到查詢sql語句,再自動改寫sql。
mybatis 攔截器
MyBatis 允許你在映射語句執(zhí)行過程中的某一點進行攔截調用。默認情況下,MyBatis 允許使用插件來攔截的方法調用包括:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
這些類中方法的細節(jié)可以通過查看每個方法的簽名來發(fā)現(xiàn),或者直接查看 MyBatis 發(fā)行包中的源代碼。 如果你想做的不僅僅是監(jiān)控方法的調用,那么你最好相當了解要重寫的方法的行為。 因為在試圖修改或重寫已有方法的行為時,很可能會破壞 MyBatis 的核心模塊。 這些都是更底層的類和方法,所以使用插件的時候要特別當心。
通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即可。
分頁插件pagehelper就是一個典型的通過攔截器去改寫SQL的。
可以看到它通過注解 @Intercepts 和簽名 @Signature 來實現(xiàn),攔截Executor執(zhí)行器,攔截所有的query查詢類方法。我們可以據(jù)此也實現(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ù)權限替換后的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(); } /** * 通過注解判斷是否需要限制數(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; } } }
順便加了個注解 @SqlLimit,在mapper方法上加了此注解才進行數(shù)據(jù)權限過濾。同時注解有兩個屬性,
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface SqlLimit { /** * sql表別名 * @return */ String alis() default ""; /** * 通過此列名進行限制 * @return */ String columnName() default ""; }
columnName表示通過此列名進行限制,一般來說一個系統(tǒng),各表當中的此列是統(tǒng)一的,可以忽略。
alis用于標注sql表別名,如 針對sql select * from tablea as a left join tableb as b on a.id = b.id
進行改寫,如果不知道表別名,會直接在后面拼接 where dpt_id = #{dptId}
,那此SQL就會錯誤的,通過別名 @SqlLimit(alis = "a")
就可以知道需要拼接的是 where a.dpt_id = #{dptId}
執(zhí)行結果
原SQL:select * from person, 數(shù)據(jù)權限替換后的SQL:select * from person where dpt_id = 234
原SQL:select * from person where id > 1, 數(shù)據(jù)權限替換后的SQL:select * from person where dpt_id = 234 and id > 1
但是在使用PageHelper進行分頁的時候還是有問題。
可以看到先執(zhí)行了_COUNT方法也就是PageHelper,再執(zhí)行了自定義的攔截器。
在我們的業(yè)務方法中注入SqlSessionFactory
@Autowired@Lazyprivate List<SqlSessionFactory> sqlSessionFactoryList;
@Autowired @Lazy private List<SqlSessionFactory> sqlSessionFactoryList;
PageInterceptor為1,自定義攔截器為0,跟order相反,PageInterceptor優(yōu)先級更高,所以越先執(zhí)行。
mybatis攔截器優(yōu)先級
@Order
通過@Order控制PageInterceptor和MySqlInterceptor可行嗎?
將MySqlInterceptor的加載優(yōu)先級調到最高,但測試證明依然不行。
定義3個類
@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)先級從低到高。順序預期的執(zhí)行順序應該是相反的:
00002 init
00001 init
00000 init
但事實上執(zhí)行的順序是
00000 init
00001 init
00002 init
@Order 不控制實例化順序,只控制執(zhí)行順序。@Order 只跟特定一些注解生效 如:@Compent @Service @Aspect … 不生效的如: @WebFilter
所以這里達不到預期效果。
@Priority 類似,同樣不行。
@DependsOn
使用此注解將當前類將在依賴類實例化之后再執(zhí)行實例化。
在MySqlInterceptor上標記@DependsOn("queryInterceptor")
啟動報錯,這個時候queryInterceptor還沒有實例化對象。
@PostConstruct
@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,并且只會被服務器執(zhí)行一次。在同一個類里,執(zhí)行順序為順序如下:Constructor > @Autowired > @PostConstruct。
但它也不能保證不同類的執(zhí)行順序。
PageHelper的springboot start也是通過這個來初始化攔截器的。
ApplicationRunner
在當前springboot容器加載完成后執(zhí)行,那么這個時候pagehelper的攔截器已經(jīng)加入,在這個時候加入自定義攔截器,就能達到我們想要的效果。
仿照PageHelper來寫
@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í)行,可以看到自定義攔截器在攔截器鏈當中下標變?yōu)榱?(優(yōu)先級與order剛好相反)
后臺打印結果,達到了預期效果。
到此這篇關于mybatis攔截器實現(xiàn)數(shù)據(jù)權限項目實踐的文章就介紹到這了,更多相關mybatis 數(shù)據(jù)權限內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- mybatis攔截器實現(xiàn)數(shù)據(jù)庫數(shù)據(jù)權限隔離方式
- MyBatis-Plus數(shù)據(jù)權限插件的簡單使用
- MybatisPlus實現(xiàn)數(shù)據(jù)權限隔離的示例詳解
- MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的方法
- Mybatis攔截器實現(xiàn)數(shù)據(jù)權限詳解
- Mybatis-plus通過添加攔截器實現(xiàn)簡單數(shù)據(jù)權限
- mybatis-plus數(shù)據(jù)權限實現(xiàn)代碼
- MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的示例
- Mybatis攔截器實現(xiàn)數(shù)據(jù)權限的示例代碼
- Mybatis自定義攔截器實現(xiàn)權限功能
相關文章
Java?根據(jù)XPATH批量替換XML節(jié)點中的值
這篇文章主要介紹了Java根據(jù)XPATH批量替換XML節(jié)點中的值,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09