找回密码
 立即注册

mybatisplus添加数据权限过滤(自定义拦截器,sql拦截)

2022-7-23 22:30:51 · 站长社区
添加数据权限过滤监听类
  1. import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
  2. import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
  3. import org.apache.ibatis.executor.statement.StatementHandler;
  4. import org.apache.ibatis.mapping.BoundSql;
  5. import org.apache.ibatis.mapping.MappedStatement;
  6. import org.apache.ibatis.mapping.SqlCommandType;
  7. import org.apache.ibatis.plugin.Interceptor;
  8. import org.apache.ibatis.plugin.Invocation;
  9. import org.apache.ibatis.plugin.Plugin;
  10. import org.apache.ibatis.reflection.MetaObject;
  11. import org.apache.ibatis.reflection.SystemMetaObject;

  12. import javax.sql.DataSource;
  13. import java.util.Properties;

  14. //@Slf4j
  15. //@AllArgsConstructor
  16. //@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
  17. //@Component
  18. public class DataScopeInterceptor  extends AbstractSqlParserHandler implements Interceptor {
  19. private DataSource dataSource;

  20. @Override
  21. public Object intercept(Invocation invocation) throws Throwable {
  22. StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
  23. MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
  24. this.sqlParser(metaObject);
  25. // 先判断是不是SELECT操作 不是直接过滤
  26. MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
  27. if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
  28. return invocation.proceed();
  29. }
  30. BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
  31. // 执行的SQL语句
  32. String originalSql = boundSql.getSql();
  33. // SQL语句的参数
  34. Object parameterObject = boundSql.getParameterObject();

  35. originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + 1 + " in (" + 2 + ")";
  36. metaObject.setValue("delegate.boundSql.sql", originalSql);
  37. return invocation.proceed();

  38. }

  39. /**

  40. * 生成拦截对象的代理
  41. *
  42. * @param target 目标对象
  43. * @return 代理对象
  44.   */
  45.   @Override
  46.   public Object plugin(Object target) {
  47.   if (target instanceof StatementHandler) {
  48.   return Plugin.wrap(target, this);
  49.   }
  50.   return target;
  51.   }

  52. /**

  53. * mybatis配置的属性
  54. *
  55. * @param properties mybatis配置的属性
  56.   */
  57.   @Override
  58.   public void setProperties(Properties properties) {

  59. }

  60. /**

  61. * 查找参数是否包括DataScope对象
  62. *
  63. * @param parameterObj 参数列表
  64. * @return DataScope
  65.   */

  66. //    private DataScope findDataScopeObject(Object parameterObj) {
  67. //        if (parameterObj instanceof DataScope) {
  68. //            return (DataScope) parameterObj;
  69. //        } else if (parameterObj instanceof Map) {
  70. //            for (Object val : ((Map) parameterObj).values()) {
  71. //                if (val instanceof DataScope) {
  72. //                    return (DataScope) val;
  73. //                }
  74. //            }
  75. //        }
  76. //        return null;
  77. //    }
  78. }
  79. 以下代码添加至mybatisplus配置文件

  80. /**
  81.      * 数据权限插件
  82.      *
  83.      * @return DataScopeInterceptor
  84.      */
  85.     @Bean
  86.     @ConditionalOnMissingBean
  87.     public DataScopeInterceptor dataScopeInterceptor(DataSource dataSource) {
  88.         return new DataScopeInterceptor(dataSource);
  89.     }
复制代码


全部评论 0

添加数据权限过滤监听类
  1. import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
  2. import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
  3. import org.apache.ibatis.executor.statement.StatementHandler;
  4. import org.apache.ibatis.mapping.BoundSql;
  5. import org.apache.ibatis.mapping.MappedStatement;
  6. import org.apache.ibatis.mapping.SqlCommandType;
  7. import org.apache.ibatis.plugin.Interceptor;
  8. import org.apache.ibatis.plugin.Invocation;
  9. import org.apache.ibatis.plugin.Plugin;
  10. import org.apache.ibatis.reflection.MetaObject;
  11. import org.apache.ibatis.reflection.SystemMetaObject;

  12. import javax.sql.DataSource;
  13. import java.util.Properties;

  14. //@Slf4j
  15. //@AllArgsConstructor
  16. //@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
  17. //@Component
  18. public class DataScopeInterceptor  extends AbstractSqlParserHandler implements Interceptor {
  19. private DataSource dataSource;

  20. @Override
  21. public Object intercept(Invocation invocation) throws Throwable {
  22. StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
  23. MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
  24. this.sqlParser(metaObject);
  25. // 先判断是不是SELECT操作 不是直接过滤
  26. MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
  27. if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
  28. return invocation.proceed();
  29. }
  30. BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
  31. // 执行的SQL语句
  32. String originalSql = boundSql.getSql();
  33. // SQL语句的参数
  34. Object parameterObject = boundSql.getParameterObject();

  35. originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + 1 + " in (" + 2 + ")";
  36. metaObject.setValue("delegate.boundSql.sql", originalSql);
  37. return invocation.proceed();

  38. }

  39. /**

  40. * 生成拦截对象的代理
  41. *
  42. * @param target 目标对象
  43. * @return 代理对象
  44.   */
  45.   @Override
  46.   public Object plugin(Object target) {
  47.   if (target instanceof StatementHandler) {
  48.   return Plugin.wrap(target, this);
  49.   }
  50.   return target;
  51.   }

  52. /**

  53. * mybatis配置的属性
  54. *
  55. * @param properties mybatis配置的属性
  56.   */
  57.   @Override
  58.   public void setProperties(Properties properties) {

  59. }

  60. /**

  61. * 查找参数是否包括DataScope对象
  62. *
  63. * @param parameterObj 参数列表
  64. * @return DataScope
  65.   */

  66. //    private DataScope findDataScopeObject(Object parameterObj) {
  67. //        if (parameterObj instanceof DataScope) {
  68. //            return (DataScope) parameterObj;
  69. //        } else if (parameterObj instanceof Map) {
  70. //            for (Object val : ((Map) parameterObj).values()) {
  71. //                if (val instanceof DataScope) {
  72. //                    return (DataScope) val;
  73. //                }
  74. //            }
  75. //        }
  76. //        return null;
  77. //    }
  78. }
  79. 以下代码添加至mybatisplus配置文件

  80. /**
  81.      * 数据权限插件
  82.      *
  83.      * @return DataScopeInterceptor
  84.      */
  85.     @Bean
  86.     @ConditionalOnMissingBean
  87.     public DataScopeInterceptor dataScopeInterceptor(DataSource dataSource) {
  88.         return new DataScopeInterceptor(dataSource);
  89.     }
复制代码


热门推荐
您需要登录后才可以回帖 立即登录
说说你的想法......
0
0
0
返回顶部