SpringBoot防止大量请求攻击

SpringBoot防止大量请求攻击我们使用Jmeter测试同学的网站时,就会出现网站无法访问,403等错误。Anerroroccurred.Sorry,thepageyouarelookingforiscurrentlyunavailable.Pleasetryagainlater.Ifyouarethesystemadministratorofthisresourcethenyoushouldchecktheerrorlogfordetails.Faithfull

大家好,又见面了,我是你们的朋友全栈君。

我们使用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://javaforall.net/162102.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Windows 批处理(bat)语法大全

    Windows 批处理(bat)语法大全本文是学习bat整理的笔记,由于内容较多,建议结合右侧文章大纲查看。%~dp0[获取当前路径]%~dp0 “d”为Drive的缩写,即为驱动器,磁盘、“p”为Path缩写,即为路径,目录cd%~dp0:进入批处理所在目录cd%~dp0bin\:进入批处理所在目录的bin目录示例这个示例在win10x64测试正常::作用:以管理员身份安装Apached:c…

    2022年7月21日
    20
  • Firefox浏览器怎么安装adobe flash player插件

    Firefox浏览器怎么安装adobe flash player插件

    2021年10月26日
    90
  • SPSS作业-卡方检验-列联表[通俗易懂]

    SPSS作业-卡方检验-列联表[通俗易懂]作业要求:某厂生产三种类型啤酒:淡啤酒、普通啤酒、黑啤酒。根据下列资料检验男性与女性饮酒者啤酒偏好是否相同。 作业数据: 淡啤 普啤 黑啤 合计 男性 20 40 20 80 女性 30

    2022年5月16日
    36
  • 手动ghost备份系统步骤_手动ghost备份图解

    手动ghost备份系统步骤_手动ghost备份图解备份前我们需要ghost,在此我提供下,在压缩文件下找到ghost百度网盘:http://pan.baidu.com/s/1mh77iWS 密码:ivxq进入ghost界面以后,按回车键,进入下一个操作界面。如下图所示:使用键盘上的方向键依次选择“Local”(本机)“Partition”(分区)“ToImage”(到镜像)然后

    2025年9月22日
    4
  • 基数排序中的LSD方法和MSD方法

    基数排序中的LSD方法和MSD方法最高位优先(MostSignificantDigitfirst)法,简称MSD法:先按k1排序分组,同一组中记录,关键码k1相等,再对各组按k2排序分成子组,之后,对后面的关键码继续这样的排序分组,直到按最次位关键码kd对各子组排序后。再将各组连接起来,便得到一个有序序列。最低位优先(LeastSignificantDigitfirst)法,简称LSD法:先从kd开始排序,再对

    2022年6月3日
    51
  • pycharm注释快捷键Ctrl+/「建议收藏」

    pycharm注释快捷键Ctrl+/「建议收藏」行注释/取消行注释:Ctrl+/块注释:Ctrl+Shift+/

    2022年8月25日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号