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 + Redis 缓存实战:@Cacheable、序列化踩坑、缓存一致性,一次讲透
spring boot·redis·缓存
Devin~Y6 小时前
大厂 Java 面试实战:从 Spring Boot 微服务到 AI RAG 音视频平台全链路解析
java·spring boot·redis·spring cloud·微服务·rag·spring ai
我登哥MVP6 小时前
SpringCloud 核心组件解析:服务注册与发现
java·spring boot·后端·spring·spring cloud·java-ee·maven
_未闻花名_6 小时前
PostgreSQL的若干扩展安装和使用
spring boot·postgresql·postgis·timescaledb·pg_cron·pgmq·zhparser
砍材农夫7 小时前
物联网实战:Spring Boot + Netty 搭建 MQTT 统一接入层
java·网络·spring boot·后端·物联网·spring
写代码的小阿帆7 小时前
英语四六级证书审核(SpringBoot+Dify+RPA)
java·spring boot
霸道流氓气质8 小时前
RabbitMQ 从零到实战:概念、配置与 Spring Boot 集成指南
spring boot·rabbitmq·java-rabbitmq
夜郎king8 小时前
SpringBoot 整合 Neo4j 实战:从零搭建经典小说知识图谱完整方案
spring boot·知识图谱·neo4j
仙俊红8 小时前
深入理解 ThreadLocal —— 从变量引用、强弱引用到 Spring Boot 实战
spring boot·python·算法
Jabes.yang8 小时前
互联网大厂Java求职面试实战解析(含技术场景与详解)
spring boot·微服务·面试·orm·技术栈·java se·jakarta ee