SpringBoot防止大量请求攻击的实现
作者:qykhhr 发布时间:2023-11-24 16:42:54
标签:SpringBoot,大量请求,攻击
我们使用Jmeter测试同学的网站时,就会出现网站无法访问,403等错误。
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访问时间限制,防止一个IP多次访问请求,导致整个网站崩溃。
自定义注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义注解,用于拦截过于频繁的请求
*/
@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 请求过于频繁请稍后再试
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秒内 允许请求3次
public R getImageList(){
return R.ok().data("appHome","hahaha");
}
}
使用python发送100次请求,可以发现请求被拦截了多少
对于注解,我们也可以不使用它,但是我们需要在 * 中写入固定的参数。
来源:https://blog.csdn.net/qq_46070108/article/details/121481623


猜你喜欢
- 本文实例为大家分享了Android系统级悬浮按钮的具体代码,供大家参考,具体内容如下具体的需求1、就是做一个系统级的悬浮按钮,就像iPhon
- Spring Security和Shiro的区别相同点1、认证功能2、授权功能3、加密功能4、会话管理5、缓存支持6、rememberMe功
- 一、项目中配置多语言多语言的实现是通过AndroidUtilCode实现的,表示感谢!项目里面有4种语言:中文,英文,德文,俄文。文件夹如下
- 本文实例讲述了C#基于XNA生成随机颜色的方法。分享给大家供大家参考。具体分析如下:确保您使用的是Microsoft.Xna.Framewo
- 简介本文用示例介绍SpringBoot如何解决雪花算法主键ID传到前端后精度丢失问题。问题描述Java后端Long类型的范围-2^63~2^
- 推荐IntelliJ IDEA 2020.2.3永久破解激活教程(亲测有效)正文开始今天将idea更新到了最新版2020.2.3,结果发现新
- 1.memchrmemchr的函数声明:void *memchr(const void *str, int c, size_t n);作用:
- 本文通过解决老王经常搞错借书人的问题,来引出行为型模式中的命令模式。为了在案例之上理解的更加透彻,我们需要了解命令模式在源码中的应用。最后指
- 饿汉式立即加载防止new对象,构造私有,写一个公共的方法返回对象占用空间,线程安全public class Singleton { &nbs
- 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。在早期面向进
- graylog配置springboot配置依赖compile group: 'de.siegmar', name: '
- 将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出D
- 首先看一看什么是装箱和拆箱?简单的来说:装箱就是值类型转换为引用类型;拆箱就是引用类型转换为值类型。值类型,包括原类型(Sbyte、Byte
- java中synchronized(同步代码块和同步方法)详解及区别问题的由来:看到这样一个面试题://下列两个方法有什么区别p
- Surface的拍照实现也是很简单,一个小demo就可以把流程看懂了。 话不多说,直接上代码布局文件<SurfaceView &nbs
- 接口的灵活性就在于“规定一个类必须做什么,而不管你如何做”。我们可以定义一个接口类型的引用变量来引用实现接口的类的实例,当这个引用调用方法时
- 一、final概述子类可以在父类的基础上改写父类内容,比如,方法重写。那么我们能不能随意的继承API中提供的类,改写其内容呢?显然这是不合适
- Groovy 简介Groovy 是构建在 JVM 上的一个轻量级却强大的动态语言,它结合了 Python、Ruby 和 Smalltalk
- 本文实例为大家分享了java实现推箱子小游戏的具体代码,供大家参考,具体内容如下二维数组二维数组:类似于二维表格(有很多层,每一层有多个房间
- 最近开发项目中,有个在屏幕上任意拖动的悬浮窗功能,其实就是利用 WindowManager的api来完成这个需求,具体的实现的功能如下:1.