欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot防止大量请求攻击的实现

程序员文章站 2022-03-13 19:51:08
我们使用jmeter测试同学的网站时,就会出现网站无法访问,403等错误。an error occurred.sorry, the page you are looking for is curren...

我们使用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次请求,可以发现请求被拦截了多少

SpringBoot防止大量请求攻击的实现

对于注解,我们也可以不使用它,但是我们需要在拦截器中写入固定的参数。

到此这篇关于springboot防止大量请求攻击的实现的文章就介绍到这了,更多相关springboot防止大量请求攻击内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!