MyBatisPlus防全表更新與刪除的實(shí)現(xiàn)示例
說(shuō)明
針對(duì) update 和 delete 語(yǔ)句,阻止惡意的全表更新和全表刪除。
實(shí)現(xiàn)方式
配置BlockAttackInnerInterceptor
攔截器
代碼
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()); // 針對(duì) update 和 delete 語(yǔ)句 作用: 阻止惡意的全表更新刪除 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 如果配置多個(gè)插件,切記分頁(yè)最后添加 // interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多數(shù)據(jù)源可以不配具體類型 否則都建議配上具體的DbType return interceptor; } }
測(cè)試
更新全表
/** * 更新全表 */ @Test public void updateAll() { User user = new User(); user.setGender(GenderEnum.MALE); mapper.update(user, null); }
刪除全表
/** * 刪除全表 */ @Test public void deleteAll() { mapper.delete(null); }
正常更新
正常更新,不受影響。
/** * 更新一條數(shù)據(jù) */ @Test public void update() { User user = new User(); user.setId(7L); user.setGender(GenderEnum.MALE); mapper.updateById(user); }
未開(kāi)啟防護(hù)前
未開(kāi)啟防護(hù)前,可以更新全表,或刪除全表。
全表更新
全表刪除
到此這篇關(guān)于MyBatisPlus防全表更新與刪除的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)MyBatisPlus防全表更新與刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis特殊字符轉(zhuǎn)義查詢實(shí)現(xiàn)
本文主要介紹了Mybatis特殊字符轉(zhuǎn)義查詢實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Java從控制臺(tái)接受輸入字符的簡(jiǎn)單方法
這篇文章主要介紹了Java從控制臺(tái)接受輸入字符的簡(jiǎn)單方法,需要的朋友可以參考下2014-02-02Debian 7 和 Debian 8 用戶安裝 Java 8的方法
Oracle Java 8 穩(wěn)定版本近期已發(fā)布,有很多新的特征變化。其中,有功能的程序支持通過(guò)“Lambda項(xiàng)目 ”,收到了一些安全更新和界面改進(jìn)上的bug修復(fù),使得開(kāi)發(fā)人員的工作更容易。2014-03-03@Schedule?如何解決定時(shí)任務(wù)推遲執(zhí)行
這篇文章主要介紹了@Schedule?如何解決定時(shí)任務(wù)推遲執(zhí)行問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09intelliJ idea 2023 配置Tomcat 8圖文教程
這篇文章主要介紹了intelliJ idea 2023 配置Tomcat 8教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06