public class DataScopeInterceptor
extends com.baomidou.mybatisplus.extension.parser.JsqlParserSupport
implements com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
CREATE_BY_ID
创建人
|
(专用程序包) IDataSourceExpressionHandler |
dataSourceExpressionHandler |
static String |
DEPT_ID
部门
|
static String |
JOB_ID
岗位
|
| 构造器和说明 |
|---|
DataScopeInterceptor() |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
beforePrepare(org.apache.ibatis.executor.statement.StatementHandler sh,
Connection connection,
Integer transactionTimeout) |
void |
beforeQuery(org.apache.ibatis.executor.Executor executor,
org.apache.ibatis.mapping.MappedStatement ms,
Object parameter,
org.apache.ibatis.session.RowBounds rowBounds,
org.apache.ibatis.session.ResultHandler resultHandler,
org.apache.ibatis.mapping.BoundSql boundSql) |
protected net.sf.jsqlparser.expression.Expression |
builderExpression(net.sf.jsqlparser.expression.Expression currentExpression,
net.sf.jsqlparser.schema.Table table)
处理条件
|
protected void |
processDelete(net.sf.jsqlparser.statement.delete.Delete delete,
int index,
String sql,
Object obj) |
protected void |
processFromItem(net.sf.jsqlparser.statement.select.FromItem fromItem)
处理子查询等
|
protected void |
processInsert(net.sf.jsqlparser.statement.insert.Insert insert,
int index,
String sql,
Object obj) |
protected void |
processJoin(net.sf.jsqlparser.statement.select.Join join)
处理联接语句
|
protected void |
processPlainSelect(net.sf.jsqlparser.statement.select.PlainSelect plainSelect)
处理 PlainSelect
|
protected void |
processSelect(net.sf.jsqlparser.statement.select.Select select,
int index,
String sql,
Object obj)
查询sql数据权限拦截处理
针对所有查询语句做拦截,包括复杂嵌套的sql语句。
|
void |
processSelectBody(net.sf.jsqlparser.statement.select.SelectBody selectBody)
改写查询条件
|
protected void |
processUpdate(net.sf.jsqlparser.statement.update.Update update,
int index,
String sql,
Object obj) |
protected void |
processWhereSubSelect(net.sf.jsqlparser.expression.Expression where)
处理where条件内的子查询
支持如下:
1. in
2. =
3. >
4. <
5. >=
6. <=
7. <>
8.
|
parserMulti, parserSingle, processParserIDataSourceExpressionHandler dataSourceExpressionHandler
public void beforeQuery(org.apache.ibatis.executor.Executor executor,
org.apache.ibatis.mapping.MappedStatement ms,
Object parameter,
org.apache.ibatis.session.RowBounds rowBounds,
org.apache.ibatis.session.ResultHandler resultHandler,
org.apache.ibatis.mapping.BoundSql boundSql)
throws SQLException
beforeQuery 在接口中 com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptorSQLExceptionpublic void beforePrepare(org.apache.ibatis.executor.statement.StatementHandler sh,
Connection connection,
Integer transactionTimeout)
beforePrepare 在接口中 com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptorprotected void processInsert(net.sf.jsqlparser.statement.insert.Insert insert,
int index,
String sql,
Object obj)
processInsert 在类中 com.baomidou.mybatisplus.extension.parser.JsqlParserSupportprotected void processDelete(net.sf.jsqlparser.statement.delete.Delete delete,
int index,
String sql,
Object obj)
processDelete 在类中 com.baomidou.mybatisplus.extension.parser.JsqlParserSupportprotected void processUpdate(net.sf.jsqlparser.statement.update.Update update,
int index,
String sql,
Object obj)
processUpdate 在类中 com.baomidou.mybatisplus.extension.parser.JsqlParserSupportprotected void processSelect(net.sf.jsqlparser.statement.select.Select select,
int index,
String sql,
Object obj)
针对所有查询语句做拦截,包括复杂嵌套的sql语句。
会做数据权限的场景:
第一,配置了数据权限DataScopeContextHolder.setDataScope(DataScopeDto),其中这步不需要做,网关层会根据设置的数据权限做自动填充。
第二,当前线程数据权限开启DataScopeContextHolder#enable()
第三,如以下sql,
select id,a.name,type,create_time,a.update_time,deploy_version from crud_form a
join
(
select name,max(update_time) update_time
from crud_form
where name is not null and name !='' and type !='flowable' and flag =0
group by name
) b on b.name=a.name and b.update_time=a.update_time
where a.name is not null and a.name !='' and a.type !='flowable' and a.flag =0 ORDER BY a.name,a.update_time desc;
出现了内嵌查询,会拆分到每条查询语句对涉及到的所有表做数据权限处理,
处理判断字段为CREATE_BY_IDJOB_IDDEPT_ID
如果当前子查询语句的数据表没有以上三个字段,则该表会跳过数据权限处理,如果配置了部门或岗位的数据权限,数据表没有JOB_IDDEPT_ID字段
也可以做数据权限处理,前提是数据表用字段CREATE_BY_ID搂底做用户层面的筛选。
权限层级分为以下几种: 所有部门 所有岗位 当前部门以及子部门 当前岗位 用户自己创建的数据 自定义权限
processSelect 在类中 com.baomidou.mybatisplus.extension.parser.JsqlParserSupportpublic void processSelectBody(net.sf.jsqlparser.statement.select.SelectBody selectBody)
selectBody - 查询语句protected void processPlainSelect(net.sf.jsqlparser.statement.select.PlainSelect plainSelect)
protected void processWhereSubSelect(net.sf.jsqlparser.expression.Expression where)
支持如下: 1. in 2. = 3. > 4. < 5. >= 6. <= 7. <> 8. EXISTS 9. NOT EXISTS
前提条件: 1. 子查询必须放在小括号中 2. 子查询一般放在比较操作符的右边
where - where 条件protected void processFromItem(net.sf.jsqlparser.statement.select.FromItem fromItem)
protected void processJoin(net.sf.jsqlparser.statement.select.Join join)
protected net.sf.jsqlparser.expression.Expression builderExpression(net.sf.jsqlparser.expression.Expression currentExpression,
net.sf.jsqlparser.schema.Table table)
Copyright © 2022 jvs. All rights reserved.