java中aop实现接口访问频率限制

本文主要介绍了java中aop实现接口访问频率限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

引言

项目开发中我们有时会用到一些第三方付费的接口,这些接口的每次调用都会产生一些费用,有时会有别有用心之人恶意调用我们的接口,造成经济损失;或者有时需要对一些执行时间比较长的的接口进行频率限制,这里我就简单演示一下我的解决思路;

主要使用spring的aop特性实现功能;

代码实现

首先需要一个注解,找个注解可以理解为一个坐标,标记该注解的接口都将进行访问频率限制;

package com.yang.prevent; import java.lang.annotation.*; /** * 接口防刷注解 */ @Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Prevent { /** * 限制的时间值(秒)默认60s */ long value() default 60; /** * 限制规定时间内访问次数,默认只能访问一次 */ long times() default 1; /** * 提示 */ String message() default ""; /** * 策略 */ PreventStrategy strategy() default PreventStrategy.DEFAULT; } 

value就是限制周期,times是在一个周期内访问次数,message是访问频率过多时的提示信息,strategy就是一个限制策略,是自定义的,如下:

package com.yang.prevent; /** * 防刷策略枚举 */ public enum PreventStrategy { /** * 默认(60s内不允许再次请求) */ DEFAULT } 

下面就是aop拦截的具体代码:

package com.yang.prevent; import com.yang.common.StatusCode; import com.yang.constant.redis.RedisKey; import com.yang.exception.BusinessException; import com.yang.utils.IpUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.Objects; import java.util.concurrent.TimeUnit; /** * 防刷切面实现类 */ @Aspect @Component public class PreventAop { @Resource private RedisTemplate redisTemplate; /** * 切入点 */ @Pointcut("@annotation(com.yang.prevent.Prevent)") public void pointcut() {} /** * 处理前 */ @Before("pointcut()") public void joinPoint(JoinPoint joinPoint) throws Exception { // 获取调用者ip RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest(); String userIP = IpUtils.getUserIP(httpServletRequest); // 获取调用接口方法名 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = joinPoint.getTarget().getClass().getMethod( methodSignature.getName(), methodSignature.getParameterTypes()); // 获取该接口方法 String methodFullName = method.getDeclaringClass().getName() + method.getName(); // 获取到方法名 Prevent preventAnnotation = method.getAnnotation(Prevent.class); // 获取该接口上的prevent注解(为了使用该注解内的参数) // 执行对应策略 entrance(preventAnnotation, userIP, methodFullName); } /** * 通过prevent注册判断执行策略 * @param prevent 该接口的prevent注解对象 * @param userIP 访问该接口的用户ip * @param methodFullName 该接口方法名 */ private void entrance(Prevent prevent, String userIP, String methodFullName) throws Exception { PreventStrategy strategy = prevent.strategy(); // 获取校验策略 if (Objects.requireNonNull(strategy) == PreventStrategy.DEFAULT) { // 默认就是default策略,执行default策略方法 defaultHandle(userIP, prevent, methodFullName); } else { throw new BusinessException(StatusCode.FORBIDDEN, "无效的策略"); } } /** * Default测试执行方法 * @param userIP 访问该接口的用户ip * @param prevent 该接口的prevent注解对象 * @param methodFullName 该接口方法名 */ private void defaultHandle(String userIP, Prevent prevent, String methodFullName) throws Exception { String base64StrIP = toBase64String(userIP); // 加密用户ip(避免ip存在一些特殊字符作为redis的key不合法) long expire = prevent.value(); // 获取访问限制时间 long times = prevent.times(); // 获取访问限制次数 // 限制特定时间内访问特定次数 long count = redisTemplate.opsForValue().increment( RedisKey.PREVENT_METHOD_NAME + base64StrIP + ":" + methodFullName, 1); // 访问次数+1 if (count == 1) { // 如果访问次数为1,则重置访问限制时间(即redis超时时间) redisTemplate.expire( RedisKey.PREVENT_METHOD_NAME + base64StrIP + ":" + methodFullName, expire, TimeUnit.SECONDS); } if (count > times) { // 如果访问次数超出访问限制次数,则禁止访问 // 如果有限制信息则使用限制信息,没有则使用默认限制信息 String errorMessage = !StringUtils.isEmpty(prevent.message()) ? prevent.message() : expire + "秒内不允许重复请求"; throw new BusinessException(StatusCode.FORBIDDEN, errorMessage); } } /** * 对象转换为base64字符串 * @param obj 对象值 * @return base64字符串 */ private String toBase64String(String obj) throws Exception { if (StringUtils.isEmpty(obj)) { return null; } Base64.Encoder encoder = Base64.getEncoder(); byte[] bytes = obj.getBytes(StandardCharsets.UTF_8); return encoder.encodeToString(bytes); } } 

注释写的很清楚了,这里我简单说一下关键方法defaultHandle:

1,首先加密ip,原因就是避免ip存在一些特殊字符作为redis的key不合法,该ip是组成redis主键的一部分,redis主键格式为:polar:prevent:加密ip:方法名

这样就能区分不同ip,同一ip下区分不同方法的访问频率;

2,expire和times都是从@Prevent注解中获取的参数,默认是60s内最多访问1次,可以自定义;

3,然后接口访问次数+1(该设备ip下),如果该接口访问次数为1,则说明这是这个ip第一次访问该接口,或者是该接口的频率限制已经解除,即该接口访问次数+1前redis中没有该ip对应接口的限制记录,所以需要重新设置对应超时时间,表示新的一轮频率限制开始;如果访问次数超过最大次数,则禁止访问该接口,直到该轮频率限制结束,redis缓存的记录超时消失,才可以再次访问该接口;

这个方法理解了其他就不难了,其他方法就是给这个方法传参或者作为校验或数据处理的;

下面测试一下,分别标记两个接口,一个使用@Prevent默认参数,一个使用自定义参数:

image-20230212013539520

调用getToleById接口,意思是60s内只能调用该接口1次:

第一次调用成功,redis键值为1

image-20230212014446080

image-20230212014507651

第二次失败,需要等60s

image-20230212014328928

redis键值变成了2

image-20230212014419442

getRoleList是自定义参数,意思是20s内最多只能访问该接口5次:

未超出频率限制

image-20230212014612633

image-20230212014635266

超出频率限制

image-20230212014658229

image-20230212014707288

总体流程就是这样了,aop理解好了不难,也比较实用,可以在自己项目中使用;

到此这篇关于java中aop实现接口访问频率限制的文章就介绍到这了,更多相关java aop接口访问频率限制内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是java中aop实现接口访问频率限制的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java