您的位置:首页 > 财经 > 产业 > 网络视频下载_网络平台制作软件教程_品牌推广包括哪些内容_泰安seo培训

网络视频下载_网络平台制作软件教程_品牌推广包括哪些内容_泰安seo培训

2024/10/6 6:02:21 来源:https://blog.csdn.net/qq_42108331/article/details/142655756  浏览:    关键词:网络视频下载_网络平台制作软件教程_品牌推广包括哪些内容_泰安seo培训
网络视频下载_网络平台制作软件教程_品牌推广包括哪些内容_泰安seo培训

说明:之前写过一篇使用Redis接口访问的博客,如下。最近有相关需求,把代码拿出来后,做了一些优化,挺有意思的,本文介绍在原基础上

  • 使用Redis实现接口防抖

优化

总的来说,这次使用Redis实现接口防抖,增加了一个时间段参数,可以限制接口在某个时间段内,访问不能超过多少次。如下:

(自定义注解,打在接口上)

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 自定义注解*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LimitAccess {/*** 限制访问的key* @return*/String key();/*** 限制访问次数* @return*/int times();/*** 时间段* @return*/int duration();
}

(切面,实现限制访问)

import com.hezy.annotation.LimitAccess;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;/*** AOP类(通知类)*/
@Component
@Aspect
@Log4j2
public class LimitAspect {@Value("${access.enable:false}")private boolean enable;@Autowiredprivate RedisTemplate redisTemplate;@Pointcut("@annotation(com.hezy.annotation.LimitAccess)")public void pt(){};@Around("pt()")public Object aopAround(ProceedingJoinPoint pjp) throws Throwable {// 设置一个开关,控制是否执行if (!enable) {return pjp.proceed();}// 获取切入点上面的自定义注解Signature signature = pjp.getSignature();MethodSignature methodSignature = (MethodSignature) signature;// 获取方法上面的注解LimitAccess limitAccess = methodSignature.getMethod().getAnnotation(LimitAccess.class);// 获取注解上面的属性值int limit = limitAccess.times();String key = limitAccess.key();int duration = limitAccess.duration();// 递增键的值,如果键不存在则初始化为1Long currentCount = redisTemplate.opsForValue().increment(key, 1);// 如果键是新创建的,设置过期时间if (currentCount != null && currentCount == 1) {redisTemplate.expire(key, duration, TimeUnit.SECONDS);}// 检查是否超过限制if (currentCount != null && currentCount > limit) {log.info("访问过于频繁: " + pjp.toLongString());throw new RuntimeException("访问过于频繁");}return pjp.proceed();}
}

(使用,在对应的接口上,打上注解,填上数值,如下表示,1秒内不能访问超过3次)

    @LimitAccess(key = "test", times = 3, duration = 1)@GetMappingpublic String test() {return demoService.test();}

另外,在代码中加了一个开关,可在配置文件中设置此配置,表示开启或者关闭,默认是关闭的

access:enable: true

启动项目,测试一下

在这里插入图片描述

思考

以上代码,有两点需要思考:

  • 注解能不能加在Service层的方法上,加了有没有用?另外加了会不会让声明式事务失效?

  • 这个限制,没有到用户的维度,也就是说所有的用户,只要在一个时间段内访问超过次数就限制,这显然是不行的。有什么办法吗?

第一点,我测试过,注解可以加在Service层方法上,是可以的,不会让事务失效,如下:

    @LimitAccess(key = "deleteUserById", times = 3, duration = 1)@Transactional@Overridepublic void deleteUserById(Integer id) {// 删除用户userMapper.deleteUserById(id);int i = 1 / 0;// 删除用户对应的角色userMapper.deleteUserRoleMapper(id);}

接口限制,事务,都生效了。

第二点,这确实是个问题,要做到针对用户层面的接口限制是必须的,不然有一个用户恶意刷新,其他用户都用不了了,这怎么可以。要解决这个问题,首先要拿到当前操作用的标识,用户名、用户ID,然后再存入key的时候,拼接上这个标识作为key。

	String key = limitAccess.key() + 用户标识(用户名、用户id);

获取当前用户信息,参考下面这篇文章:

  • 获取当前用户信息的几种方式

如果你不是自己从0开发项目,一般成熟的项目都会有获取当前操作用户信息的方式的。实在不行,你让前端把用户id作为参数传给你,你在切面里获取这个用户id都可以。

总结

本文是对之前用Redis接口访问的优化,以及对两个问题的考虑,希望能对大家有启发。

获取源码:https://github.com/HeZhongYing/limit_access_demo

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com