当前位置: 首页 > news >正文

网站建设套餐内容网络宣传平台有哪些

网站建设套餐内容,网络宣传平台有哪些,阿里巴巴网站运营,工厂办公室装修设计引言 项目开发中我们有时会用到一些第三方付费的接口,这些接口的每次调用都会产生一些费用,有时会有别有用心之人恶意调用我们的接口,造成经济损失;或者有时需要对一些执行时间比较长的的接口进行频率限制,这里我就简…

引言

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

主要使用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 {@Resourceprivate RedisTemplate<String, Long> redisTemplate;/*** 切入点*/@Pointcut("@annotation(com.yang.prevent.Prevent)")public void pointcut() {}/*** 处理前*/@Before("pointcut()")public void joinPoint(JoinPoint joinPoint) throws Exception {// 获取调用者ipRequestAttributes 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); // 访问次数+1if (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理解好了不难,也比较实用,可以在自己项目中使用;

http://www.zhongyajixie.com/news/37320.html

相关文章:

  • 怎么做外贸网站优化杭州seo
  • wordpress会员网站电商培训内容有哪些
  • 西安专业网站建设公司哪家好淘宝店铺运营推广
  • 成都专业网站建设价格低抖音代运营大概多少钱一个月
  • 做微信推送的网站搜索引擎优化培训班
  • 济南智能网站建设费用百度seo最成功的优化
  • 湖南众诚建设 官方网站路由器优化大师
  • 长沙公司做网站大概多少钱品牌推广外包
  • 定制高端网站的公司网站收录查询爱站
  • 1920网站banner尺寸百度推广官网登录
  • 政府办工作网站建设工作计划成都官网seo费用
  • 如何建好一个网站市场推广的方法和规划
  • 廊坊外贸网站建设seo零基础入门到精通200讲
  • 海城做网站安装百度到桌面
  • 只做山寨的网站友情链接软件
  • 简阳网站建设百度关键词seo排名软件
  • 英文商城网站环球军事网最新消息
  • 建设银行的网站是多少网络营销推广方法和手段
  • 公司网站设计案例网站关键词优化的步骤和过程
  • 影楼化妆师工资一般多少windows优化大师的作用
  • 七牛云 微信 打开 wordpress网站seo优化服务
  • 本网站建设于美利坚合众国百度高级搜索怎么用
  • www开头网站怎么做如何进行网站性能优化
  • 杭州网站建设有限公司百度首页排名优化服务
  • 锦州北京网站建设百度收录关键词
  • 北京疫情最新消息2023年青岛seo排名收费
  • 网站运营怎么做网络推广
  • 济南网站建设泰观网络怎么样把广告做在百度上
  • 直播教育网站建设网页搜索优化seo
  • 全站仪快速建站如何自己弄个免费网站