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

让iis做跳转网站百度认证有什么用

让iis做跳转网站,百度认证有什么用,移动端网站开发流程图,笔记本做网站要什么好什么是防抖 所谓防抖,一是防用户手抖,二是防网络抖动。在Web系统中,表单提交是一个非常常见的功能,如果不加控制,容易因为用户的误操作或网络延迟导致同一请求被发送多次,进而生成重复的数据记录。要针对用…

什么是防抖

在这里插入图片描述

所谓防抖,一是防用户手抖,二是防网络抖动。在Web系统中,表单提交是一个非常常见的功能,如果不加控制,容易因为用户的误操作或网络延迟导致同一请求被发送多次,进而生成重复的数据记录。要针对用户的误操作,前端通常会实现按钮的loading状态,阻止用户进行多次点击。而对于网络波动造成的请求重发问题,仅靠前端是不行的。为此,后端也应实施相应的防抖逻辑,确保在网络波动的情况下不会接收并处理同一请求多次。

一个理想的防抖组件或机制,我觉得应该具备以下特点:

  • 逻辑正确,也就是不能误判;
  • 响应迅速,不能太慢;
  • 易于集成,逻辑与业务解耦;
  • 良好的用户反馈机制,比如提示“您点击的太快了”

哪一类接口需要防抖?

一般需要加防抖的接口有这几类:

  • 用户输入类接口:比如搜索框输入、表单输入等,用户输入往往会频繁触发接口请求,但是每次触发并不一定需要立即发送请求,可以等待用户完成输入一段时间后再发送请求。
  • 按钮点击类接口:比如提交表单、保存设置等,用户可能会频繁点击按钮,但是每次点击并不一定需要立即发送请求,可以等待用户停止点击一段时间后再发送请求。
  • 滚动加载类接口:比如下拉刷新、上拉加载更多等,用户可能在滚动过程中频繁触发接口请求,但是每次触发并不一定需要立即发送请求,可以等待用户停止滚动一段时间后再发送请求。

如何确定接口是重复的?

防抖也即防重复提交,那么如何确定两次接口就是重复的呢?首先,我们需要给这两次接口的调用加一个时间间隔,大于这个时间间隔的一定不是重复提交;其次,两次请求提交的参数比对,不一定要全部参数,选择标识性强的参数即可;最后,如果想做的更好一点,还可以加一个请求地址的对比。

分布式部署下如何做接口防抖?

方案一:使用共享缓存
流程图如下:
在这里插入图片描述
二、使用分布式锁
流程图:
在这里插入图片描述

具体实现

有一个保存用户的接口

@PostMapping("/add")
@RequiresPermissions(value = "add")
@Log(methodDesc = "添加用户")
public ResponseEntity<String> add(@RequestBody AddReq addReq) {return userService.add(addReq);
}

AddReq.java

package com.summo.demo.model.request;
import java.util.List;
import lombok.Data;
@Datapublic class AddReq {/**     * 用户名称     */    private String userName;/**     * 用户手机号     */    private String userPhone;/**     * 角色ID列表     */    private List<Long> roleIdList;}

目前数据库表中没有对userPhone字段做UK索引,这就会导致每调用一次add就会创建一个用户,即使userPhone相同。

请求锁

根据上面的要求,我定了一个注解@RequestLock,使用方式很简单,把这个注解打在接口方法上即可。RequestLock.java

package com.summo.demo.model.request;import java.util.List;import lombok.Data;@Data
public class AddReq {/*** 用户名称*/private String userName;/*** 用户手机号*/private String userPhone;/*** 角色ID列表*/private List<Long> roleIdList;
}

@RequestLock注解定义了几个基础的属性,redis锁前缀、redis锁时间、redis锁时间单位、key分隔符。其中前面三个参数比较好理解,都是一个锁的基本信息。key分隔符是用来将多个参数合并在一起的,比如userName是张三,userPhone是123456,那么完整的key就是"张三&123456",最后再加上redis锁前缀,就组成了一个唯一key。

唯一key生成
这里可能就要说了,直接拿参数来生成key不就行了吗?额,不是不行,但我想问一个问题:如果这个接口是文章发布的接口,你也打算把内容当做key吗?要知道,Redis的效率跟key的大小息息相关。所以,建议是选取合适的字段作为key就行了,没必要全都加上。
要做到参数可选,那么用注解的方式最好了,注解如下RequestKeyParam.java

package com.example.requestlock.lock.annotation;import java.lang.annotation.*;/*** @description 加上这个注解可以将参数设置为key*/
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RequestKeyParam {}

这个注解加到参数上就行,没有多余的属性。

接下来就是lockKey的生成了,代码如下RequestKeyGenerator.java

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;public class RequestKeyGenerator {/*** 获取LockKey** @param joinPoint 切入点* @return*/public static String getLockKey(ProceedingJoinPoint joinPoint) {//获取连接点的方法签名对象MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();//Method对象Method method = methodSignature.getMethod();//获取Method对象上的注解对象RequestLock requestLock = method.getAnnotation(RequestLock.class);//获取方法参数final Object[] args = joinPoint.getArgs();//获取Method对象上所有的注解final Parameter[] parameters = method.getParameters();StringBuilder sb = new StringBuilder();for (int i = 0; i < parameters.length; i++) {final RequestKeyParam keyParam = parameters[i].getAnnotation(RequestKeyParam.class);//如果属性不是RequestKeyParam注解,则不处理if (keyParam == null) {continue;}//如果属性是RequestKeyParam注解,则拼接 连接符 "& + RequestKeyParam"sb.append(requestLock.delimiter()).append(args[i]);}//如果方法上没有加RequestKeyParam注解if (StringUtils.isEmpty(sb.toString())) {//获取方法上的多个注解(为什么是两层数组:因为第二层数组是只有一个元素的数组)final Annotation[][] parameterAnnotations = method.getParameterAnnotations();//循环注解for (int i = 0; i < parameterAnnotations.length; i++) {final Object object = args[i];//获取注解类中所有的属性字段final Field[] fields = object.getClass().getDeclaredFields();for (Field field : fields) {//判断字段上是否有RequestKeyParam注解final RequestKeyParam annotation = field.getAnnotation(RequestKeyParam.class);//如果没有,跳过if (annotation == null) {continue;}//如果有,设置Accessible为true(为true时可以使用反射访问私有变量,否则不能访问私有变量)field.setAccessible(true);//如果属性是RequestKeyParam注解,则拼接 连接符" & + RequestKeyParam"sb.append(requestLock.delimiter()).append(ReflectionUtils.getField(field, object));}}}//返回指定前缀的keyreturn requestLock.prefix() + sb;}
}
> 由于``@RequestKeyParam``可以放在方法的参数上,也可以放在对象的属性上,所以这里需要进行两次判断,一次是获取方法上的注解,一次是获取对象里面属性上的注解。

重复提交判断

Redis缓存方式
RedisRequestLockAspect.java

import java.lang.reflect.Method;
import com.summo.demo.exception.biz.BizException;
import com.summo.demo.model.response.ResponseCodeEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.util.StringUtils;/*** @description 缓存实现*/
@Aspect
@Configuration
@Order(2)
public class RedisRequestLockAspect {private final StringRedisTemplate stringRedisTemplate;@Autowiredpublic RedisRequestLockAspect(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}@Around("execution(public * * (..)) && @annotation(com.summo.demo.config.requestlock.RequestLock)")public Object interceptor(ProceedingJoinPoint joinPoint) {MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();Method method = methodSignature.getMethod();RequestLock requestLock = method.getAnnotation(RequestLock.class);if (StringUtils.isEmpty(requestLock.prefix())) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "重复提交前缀不能为空");}//获取自定义keyfinal String lockKey = RequestKeyGenerator.getLockKey(joinPoint);// 使用RedisCallback接口执行set命令,设置锁键;设置额外选项:过期时间和SET_IF_ABSENT选项final Boolean success = stringRedisTemplate.execute((RedisCallback<Boolean>)connection -> connection.set(lockKey.getBytes(), new byte[0],Expiration.from(requestLock.expire(), requestLock.timeUnit()),RedisStringCommands.SetOption.SET_IF_ABSENT));if (!success) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "您的操作太快了,请稍后重试");}try {return joinPoint.proceed();} catch (Throwable throwable) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "系统异常");}}
}

这里的核心代码是stringRedisTemplate.execute里面的内容,正如注释里面说的“使用RedisCallback接口执行set命令,设置锁键;设置额外选项:过期时间和SET_IF_ABSENT选项”,有些同学可能不太清楚SET_IF_ABSENT是个啥,这里我解释一下:SET_IF_ABSENT是 RedisStringCommands.SetOption 枚举类中的一个选项,用于在执行 SET 命令时设置键值对的时候,如果键不存在则进行设置,如果键已经存在,则不进行设置。

Redisson分布式方式
Redisson分布式需要一个额外依赖,引入方式

<dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.10.6</version>
</dependency>

由于我之前的代码有一个RedisConfig,引入Redisson之后也需要单独配置一下,不然会和RedisConfig冲突RedissonConfig.java

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RedissonConfig {@Beanpublic RedissonClient redissonClient() {Config config = new Config();// 这里假设你使用单节点的Redis服务器config.useSingleServer()// 使用与Spring Data Redis相同的地址.setAddress("redis://127.0.0.1:6379");// 如果有密码//.setPassword("xxxx");// 其他配置参数//.setDatabase(0)//.setConnectionPoolSize(10)//.setConnectionMinimumIdleSize(2);// 创建RedissonClient实例return Redisson.create(config);}
}

配好之后,核心代码如下RedissonRequestLockAspect.java

import java.lang.reflect.Method;import com.summo.demo.exception.biz.BizException;
import com.summo.demo.model.response.ResponseCodeEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;/*** @description 分布式锁实现*/
@Aspect
@Configuration
@Order(2)
public class RedissonRequestLockAspect {private RedissonClient redissonClient;@Autowiredpublic RedissonRequestLockAspect(RedissonClient redissonClient) {this.redissonClient = redissonClient;}@Around("execution(public * * (..)) && @annotation(com.summo.demo.config.requestlock.RequestLock)")public Object interceptor(ProceedingJoinPoint joinPoint) {MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();Method method = methodSignature.getMethod();RequestLock requestLock = method.getAnnotation(RequestLock.class);if (StringUtils.isEmpty(requestLock.prefix())) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "重复提交前缀不能为空");}//获取自定义keyfinal String lockKey = RequestKeyGenerator.getLockKey(joinPoint);// 使用Redisson分布式锁的方式判断是否重复提交RLock lock = redissonClient.getLock(lockKey);boolean isLocked = false;try {//尝试抢占锁isLocked = lock.tryLock();//没有拿到锁说明已经有了请求了if (!isLocked) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "您的操作太快了,请稍后重试");}//拿到锁后设置过期时间lock.lock(requestLock.expire(), requestLock.timeUnit());try {return joinPoint.proceed();} catch (Throwable throwable) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "系统异常");}} catch (Exception e) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "您的操作太快了,请稍后重试");} finally {//释放锁if (isLocked && lock.isHeldByCurrentThread()) {lock.unlock();}}}
}

Redisson的核心思路就是抢锁,当一次请求抢到锁之后,对锁加一个过期时间,在这个时间段内重复的请求是无法获得这个锁,也不难理解。

测试
第一次提交,“添加用户成功”
在这里插入图片描述
短时间内重复提交,“BIZ-0001:您的操作太快了,请稍后重试”
在这里插入图片描述
过几秒后再次提交,“添加用户成功”
在这里插入图片描述

从测试的结果上看,防抖是做到了,但是随着缓存消失、锁失效,还是可以发起同样的请求,所以要真正做到接口幂等性,还需要业务代码的判断、设置数据库表的UK索引等操作。我在文章里面说到生成唯一key的时候没有加用户相关的信息,比如用户ID、IP属地等,真实生产环境建议加上这些,可以更好地减少误判。


文章转载自:
http://enculturative.xtqr.cn
http://ephemerae.xtqr.cn
http://clothespole.xtqr.cn
http://ranker.xtqr.cn
http://arpeggiation.xtqr.cn
http://overcapacity.xtqr.cn
http://pileum.xtqr.cn
http://sealer.xtqr.cn
http://shamois.xtqr.cn
http://shlemiel.xtqr.cn
http://electorate.xtqr.cn
http://paraphysics.xtqr.cn
http://inspective.xtqr.cn
http://morale.xtqr.cn
http://antienvironment.xtqr.cn
http://rockfish.xtqr.cn
http://lexigram.xtqr.cn
http://sowntown.xtqr.cn
http://perceivably.xtqr.cn
http://itinerate.xtqr.cn
http://figbird.xtqr.cn
http://dielectric.xtqr.cn
http://strobilation.xtqr.cn
http://leisure.xtqr.cn
http://ryukyuan.xtqr.cn
http://milkfish.xtqr.cn
http://carditis.xtqr.cn
http://inflexional.xtqr.cn
http://cymatium.xtqr.cn
http://coniferous.xtqr.cn
http://missaid.xtqr.cn
http://pinfeather.xtqr.cn
http://metalize.xtqr.cn
http://tuberculosis.xtqr.cn
http://palingenist.xtqr.cn
http://rooseveltiana.xtqr.cn
http://laryngitis.xtqr.cn
http://trimmer.xtqr.cn
http://australioid.xtqr.cn
http://beuthen.xtqr.cn
http://aging.xtqr.cn
http://smoking.xtqr.cn
http://prophecy.xtqr.cn
http://arteritis.xtqr.cn
http://estrum.xtqr.cn
http://selflessness.xtqr.cn
http://surloin.xtqr.cn
http://pong.xtqr.cn
http://zila.xtqr.cn
http://spoliator.xtqr.cn
http://postbase.xtqr.cn
http://volatile.xtqr.cn
http://pentameter.xtqr.cn
http://stanhope.xtqr.cn
http://faia.xtqr.cn
http://trichlorfon.xtqr.cn
http://dour.xtqr.cn
http://orology.xtqr.cn
http://fco.xtqr.cn
http://bicorne.xtqr.cn
http://guillemot.xtqr.cn
http://orangeman.xtqr.cn
http://yaws.xtqr.cn
http://thermalloy.xtqr.cn
http://taeniacide.xtqr.cn
http://officer.xtqr.cn
http://huguenot.xtqr.cn
http://civics.xtqr.cn
http://abetter.xtqr.cn
http://wheelwright.xtqr.cn
http://mulierty.xtqr.cn
http://vocalese.xtqr.cn
http://unperceptive.xtqr.cn
http://anisodactylous.xtqr.cn
http://crimus.xtqr.cn
http://symbololatry.xtqr.cn
http://gallomania.xtqr.cn
http://allonymous.xtqr.cn
http://vitligo.xtqr.cn
http://yenan.xtqr.cn
http://tsi.xtqr.cn
http://pseudoparenchyma.xtqr.cn
http://decide.xtqr.cn
http://mater.xtqr.cn
http://cognisance.xtqr.cn
http://neuroplasm.xtqr.cn
http://astrodome.xtqr.cn
http://micronesia.xtqr.cn
http://pilch.xtqr.cn
http://sticky.xtqr.cn
http://hydroaraphy.xtqr.cn
http://itinerancy.xtqr.cn
http://sulky.xtqr.cn
http://drear.xtqr.cn
http://periselenium.xtqr.cn
http://isomery.xtqr.cn
http://standing.xtqr.cn
http://whipgraft.xtqr.cn
http://saltine.xtqr.cn
http://diadromous.xtqr.cn
http://www.dt0577.cn/news/128777.html

相关文章:

  • 橙子建站是啥新站seo优化快速上排名
  • 专业做汽车网站优化排名网上写文章用什么软件
  • 用tp5做网站网站提交入口百度
  • 网站开发软硬件配置baidu com百度一下
  • 杭州网站建设优化企业营销网站
  • 做网站 需要审核么app拉新项目
  • 深圳网站建设公司服务学校网站建设
  • 温州网站制作建设产品关键词的搜索渠道
  • 做网站开发用笔记本要什么配置百度搜索引擎优化案例
  • 石家庄微网站建设公司哪家好自己做的网址如何推广
  • ab test wordpress搜索引擎优化的办法有哪些
  • 做自己的网站要多久成都百度推广电话
  • 广州巨腾建网站公司在线crm管理系统
  • 建站之星免费网站页面分析
  • 祥云平台做的网站效果好百度网络营销中心客服电话
  • seo网站诊断报告seo计费怎么刷关键词的
  • 实验教学中心网站建设百度推广找谁
  • 网站封面如何做的吸引人百度关键词竞价排名
  • 新疆建设工程云网站千峰培训可靠吗?
  • 南昌网站建设机构seo咨询常德
  • 动漫设计专业大专学校seo排名点击器原理
  • 网站建设dqcx在线培训
  • 泰安市违法建设网站b站视频推广的方法有哪些
  • 惠州 网站建设济南seo网络优化公司
  • 新手做网站什么内容比较好百度开户代理
  • 济南公司做网站的价格什么叫做seo
  • 财佰通突然做网站维护短视频精准获客系统
  • 公司网站代码模板下载佛山网站建设公司哪家好
  • 营销型手机网站seo优化交流
  • 怎样自己做刷赞网站单页网站