SpringBoot防止大量請(qǐng)求攻擊的實(shí)現(xiàn)
我們使用Jmeter測(cè)試同學(xué)的網(wǎng)站時(shí),就會(huì)出現(xiàn)網(wǎng)站無(wú)法訪問(wèn),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訪問(wèn)時(shí)間限制,防止一個(gè)IP多次訪問(wèn)請(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;
/**
* 自定義注解,用于攔截過(guò)于頻繁的請(qǐng)求
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AccessLimit {
int seconds();
int maxCount();
boolean needLogin() default true;
}
自定義攔截器:
我采用了拋出自定義異常的方式來(lái)解決相同IP多次訪問(wèn)的問(wèn)題:
throw new DujiaoshouException(20001,"操作過(guò)于頻繁");
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)求過(guò)于頻繁請(qǐng)稍后再試
throw new DujiaoshouException(20001,"操作過(guò)于頻繁");
}
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)文章
Spring事務(wù)失效場(chǎng)景的詳細(xì)整理
Spring 事務(wù)的傳播特性說(shuō)的是,當(dāng)多個(gè)事務(wù)同時(shí)存在的時(shí)候,Spring 如何處理這些事務(wù)的特性,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)失效場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2022-02-02
springboot配置文件屬性變量引用方式${}和@@用法及區(qū)別說(shuō)明
這篇文章主要介紹了springboot配置文件屬性變量引用方式${}和@@用法及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot如何通過(guò)@Profile注解配置多環(huán)境
在Spring中,可以使用配置文件的方式來(lái)指定不同環(huán)境下所需要的配置信息,本文給大家介紹SpringBoot如何通過(guò)@Profile注解配置多環(huán)境,感興趣的朋友跟隨小編一起看看吧2023-06-06
js中去除字符串中所有的html標(biāo)簽代碼實(shí)例
這篇文章主要介紹了js中去除字符串中所有的html標(biāo)簽代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Java如何將ResultSet結(jié)果集遍歷到List中
這篇文章主要介紹了Java如何將ResultSet結(jié)果集遍歷到List中問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用
Java圖形驗(yàn)證碼,支持gif、中文、算術(shù)等類型,可用于Java Web、JavaSE等項(xiàng)目,下面就跟隨小編一起來(lái)了解一下2021-08-08
mybatis?查詢返回Map<String,Object>類型
本文主要介紹了mybatis?查詢返回Map<String,Object>類型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
SpringBoot整合Mybatis-Plus分頁(yè)失效的解決
本文主要介紹了SpringBoot整合Mybatis-Plus分頁(yè)失效的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Spring 使用Validation 驗(yàn)證框架的問(wèn)題詳解
Spring Boot在內(nèi)部通過(guò)集成hibernate-validation已經(jīng)實(shí)現(xiàn)了JSR-349驗(yàn)證規(guī)范接口,在Spring Boot項(xiàng)目中只要直接使用就行了。 一般用在Controller中用于驗(yàn)證前端傳來(lái)的參數(shù)。這篇文章給大家介紹Spring Validation 驗(yàn)證框架的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-07-07

