MyBatisPlus(二十)防全表更新与删除

说明

针对 update 和 delete 语句,阻止恶意的全表更新和全表删除。

实现方式

配置BlockAttackInnerInterceptor拦截器

代码

java 复制代码
package com.example.core.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.example.web")
public class MybatisPlusConfig {

    /**
     * 添加拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); // 针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 如果配置多个插件,切记分页最后添加
        // interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        return interceptor;
    }
}

测试

更新全表

java 复制代码
    /**
     * 更新全表
     */
    @Test
    public void updateAll() {
        User user = new User();
        user.setGender(GenderEnum.MALE);

        mapper.update(user, null);
    }

删除全表

java 复制代码
    /**
     * 删除全表
     */
    @Test
    public void deleteAll() {
        mapper.delete(null);
    }

正常更新

正常更新,不受影响。

java 复制代码
    /**
     * 更新一条数据
     */
    @Test
    public void update() {
        User user = new User();
        user.setId(7L);
        user.setGender(GenderEnum.MALE);

        mapper.updateById(user);
    }

未开启防护前

未开启防护前,可以更新全表,或删除全表。

全表更新


全表删除


相关推荐
今天的砖头有点烫手啊5 小时前
接口太慢?Spring Boot 缓存体系 @Cacheable 全链路拆解
spring boot·后端·缓存
zzzll11117 小时前
Spring Boot 入门指南:从零开始构建微服务
spring boot·后端·微服务
用户3126874877207 小时前
你的定时任务真的靠谱吗?Spring Boot @Scheduled 全链路拆解
spring boot
程序员天天困9 小时前
Spring Boot i18n 国际化实战:从资源文件到多语言接口完整指南
spring boot·后端·编程语言
孫治AllenSun1 天前
【SpringBoot】计算机网络
spring boot·后端·计算机网络
Flittly1 天前
【雕虫大技】Agent 动态 Skill 供应链安全加固(二):执行隔离沙箱实战
java·spring boot·spring
沐言人生1 天前
HelloAgentR工程手记1/100天——项目工作流搭建
spring boot·agent·vibecoding
陌上丨1 天前
Spring Boot 应用类型推断机制:一次从源码到原理的深度探索
java·spring boot·后端
云烟成雨TD1 天前
Micrometer 系列【33】Spring Boot Micrometer Metrics 自动配置模块解析
spring boot·云原生·micrometer
用户3126874877201 天前
接口太慢?Spring Boot 缓存体系 @Cacheable 全链路拆解
spring boot