SpringBoot防止大量請(qǐng)求攻擊的實(shí)現(xiàn)
我們使用Jmeter測試同學(xué)的網(wǎng)站時(shí),就會(huì)出現(xiàn)網(wǎng)站無法訪問,403等錯(cuò)誤。
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
所以我們需要加上IP訪問時(shí)間限制,防止一個(gè)IP多次訪問請(qǐng)求,導(dǎo)致整個(gè)網(wǎng)站崩潰。
自定義注解:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 自定義注解,用于攔截過于頻繁的請(qǐng)求 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface AccessLimit { int seconds(); int maxCount(); boolean needLogin() default true; }
自定義攔截器:
我采用了拋出自定義異常的方式來解決相同IP多次訪問的問題:
throw new DujiaoshouException(20001,"操作過于頻繁");
import com.qykhhr.dujiaoshouservice.exceptionhandler.DujiaoshouException; import com.qykhhr.dujiaoshouservice.mycomment.AccessLimit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.concurrent.TimeUnit; /** * 自定義攔截器 */ @Component public class AccessLimtInterceptor implements HandlerInterceptor { @Autowired private RedisTemplate redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod hm = (HandlerMethod) handler; AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if (null == accessLimit) { return true; } int seconds = accessLimit.seconds(); int maxCount = accessLimit.maxCount(); boolean needLogin = accessLimit.needLogin(); if (needLogin) { //判斷是否登錄 } String ip=request.getRemoteAddr(); String key = request.getServletPath() + ":" + ip ; Integer count = (Integer) redisTemplate.opsForValue().get(key); if (null == count || -1 == count) { redisTemplate.opsForValue().set(key, 1,seconds, TimeUnit.SECONDS); return true; } if (count < maxCount) { count = count+1; redisTemplate.opsForValue().set(key, count,0); return true; } // response 返回 json 請(qǐng)求過于頻繁請(qǐng)稍后再試 throw new DujiaoshouException(20001,"操作過于頻繁"); } return true; } }
在webconfig中配置攔截器
import com.qykhhr.dujiaoshouservice.Interceptor.AccessLimtInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * 在webconfig中配置攔截器 */ @Configuration public class MyWebMvcConfigurer extends WebMvcConfigurerAdapter { @Autowired private AccessLimtInterceptor accessLimtInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(accessLimtInterceptor); super.addInterceptors(registry); } }
在Controller前面加上注解就可以生效了
@RestController public class AppHomeController { @GetMapping("/index") @AccessLimit(seconds = 1, maxCount = 3) //1秒內(nèi) 允許請(qǐng)求3次 public R getImageList(){ return R.ok().data("appHome","hahaha"); } }
使用python發(fā)送100次請(qǐng)求,可以發(fā)現(xiàn)請(qǐng)求被攔截了多少
對(duì)于注解,我們也可以不使用它,但是我們需要在攔截器中寫入固定的參數(shù)。
到此這篇關(guān)于SpringBoot防止大量請(qǐng)求攻擊的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot防止大量請(qǐng)求攻擊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor)
mybatis中sqlsession下的四大對(duì)象是指:executor, statementHandler,parameterHandler,resultHandler對(duì)象。這篇文章主要介紹了Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor),需要的朋友可以參考下2019-04-04mybatis insert foreach循環(huán)插入方式
這篇文章主要介紹了mybatis insert foreach循環(huán)插入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot自定義路由覆蓋實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringBoot自定義路由覆蓋實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01舉例講解Java的Jackson庫中ObjectMapper類的使用
這篇文章主要介紹了舉例講解Java的Jackson庫中ObjectMapper類的使用,Jackson庫通常被用來實(shí)現(xiàn)Java的對(duì)象和JSON之間的轉(zhuǎn)換功能,需要的朋友可以參考下2016-01-01詳解Mybatis 傳遞參數(shù)類型為List的取值問題
這篇文章主要介紹了詳解Mybatis 傳遞參數(shù)類型為List的取值問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10微服務(wù)實(shí)戰(zhàn)之怎樣提升springboot服務(wù)吞吐量
這篇文章主要介紹了微服務(wù)實(shí)戰(zhàn)之怎樣提升springboot服務(wù)吞吐量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08