BlockAttackInnerInterceptor 是mybatis-plus的一个内置拦截器,用于防止恶意的全表更新或删除操作。当你添加了这个拦截器后,它会检查即将执行的 sql语句,如果有尝试进行全表更新或删除的语句,该拦截器会阻止这些操作。
<!-- mybatis-plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.2.0</version></dependency>
package com.zy.fast.admin.config;import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import net.sf.jsqlparser.statement.delete.Delete;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.ArrayList;
import java.util.List;/*** @author javachen* @description MybatisPlus分页工具类*/
@Configuration
@MapperScan("com.zy.fast.admin.moudles.*.mapper")
public class MybatisPlusConfig {@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();// 设置最大单页限制数量,默认 500 条,-1 不受限制paginationInterceptor.setLimit(500);// 开启 count 的 join 优化,只针对部分 left joinpaginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));List<ISqlParser> sqlParserList = new ArrayList<>();// 攻击 SQL 阻断解析器、加入解析链sqlParserList.add(new BlockAttackSqlParser() {@Overridepublic void processDelete(Delete delete) {//如果你想自定义做点什么,可以重写父类方法像这样子if ("sys_user_account".equals(delete.getTable().getName())) {// 自定义跳过某个表,其他关联表可以调用 delete.getTables() 判断return ;}super.processDelete(delete);}});paginationInterceptor.setSqlParserList(sqlParserList);return paginationInterceptor;}}
测试不带where条件删除时
### Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletion
### The error may exist in file [D:\fast-admin\target\classes\mapper\system\SysUserAccountMapper.xml]
### The error may involve com.zy.fast.admin.moudles.system.mapper.SysUserAccountMapper.deleteByUserId
### The error occurred while executing an update
### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletionat org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:212)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)... 116 common frames omitted