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);
    }

未开启防护前

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

全表更新


全表删除


相关推荐
Rain的Java大神之路9 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
+VX:Fegn089512 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
需要82613 小时前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat
一 乐13 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
vx-程序开发14 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
一 乐14 小时前
养老院管理系统|基于springboot + vue养老院管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
今年下半年17 小时前
SpringBoot整合金蝶中间件
spring boot·中间件·maven
字节探索17 小时前
别再只会写 Controller 了!一文吃透 Spring Boot 高级特性与项目实战
spring boot·spring
MetaLite18 小时前
全网最好的SpringBoot接口请求对象设计
java·spring boot·后端
摇滚侠19 小时前
《Spring Boot 3:高级与架构设计》第 2 章 IOC容器的高级机制 BeanFactoryPostProcessor 个人理解 6
java·spring boot·笔记·后端