SpringBoot+Aop+Redis+自定义注解来实现防止同一个ip在短时间内恶意多次请求

本文介绍了一种使用SpringBoot、Aop和Redis结合自定义注解的方法,来防止同一IP在短时间内进行多次恶意请求。通过自定义注解标记关键接口,Aop切面处理请求计数,并利用Redis存储和检查IP请求频率,从而实现防护策略。
摘要由CSDN通过智能技术生成

自定义注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface RequestLimit {

    /**
     * 允许访问的次数,默认值20
     */
    int count() default 20;

    /**
     * 时间段,单位为毫秒,默认值一分钟
     */
    long time() default 60000;
}

Aop增强类:

@Component
@Aspect
@Slf4j
public class RequestLimitAop {

    private Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisService redisService;

    @Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)")
    public void requestLimit(JoinPoint joinPoint, RequestLimit limit) throws Exception {
        Object[] args = joinPoint.getArgs();
        HttpServletRequest request = ((ServletRequestAttributes) Re
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值