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

珠海移动网站建设公司排名深圳市昊客网络科技有限公司

珠海移动网站建设公司排名,深圳市昊客网络科技有限公司,在线教育网站开发,门户网站模板免费下载若依框架实现后端防止用户重复点击 基于自定义注解、切面、Redis实现 1. 添加自定义注解: 代码放置位置:com/ruoyi/common/annotation/RepeatClick.java time: 时间默认0; unit:单位默认 秒; key: 默认空字符串 package com.ruoyi.fra…

若依框架实现后端防止用户重复点击

基于自定义注解、切面、Redis实现

1. 添加自定义注解:

代码放置位置:com/ruoyi/common/annotation/RepeatClick.java

time: 时间默认0;

unit:单位默认 秒;

key: 默认空字符串

package com.ruoyi.framework.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;/*** @author yizhi*/
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatClick {/*** 时间*/int time() default 0;/*** 时间单位,默认秒*/TimeUnit unit() default TimeUnit.SECONDS;/*** 默认会校验的数据*/String key() default "";
}

2. 添加自定义切面:

代码放置位置:com/ruoyi/framework/aspectj/RepeatClickAspect.java

基于注解和Redis实现防止重复点击

package com.ruoyi.framework.aspectj;import com.ruoyi.common.annotation.RepeatClick;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.log4j.Log4j2;
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.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;import javax.annotation.Resource;
import java.util.UUID;/*** @author yizhi*/
@Aspect
@Component
@Log4j2
public class RepeatClickAspect {@Resourceprivate RedisTemplate<String, Object> redisTemplate;/*** 方式一** @param joinPoint* @param repeatClick* @return* @throws Throwable*/@Around("@annotation(repeatClick)")public Object repeatClick(ProceedingJoinPoint joinPoint, RepeatClick repeatClick) throws Throwable {System.out.println("进入切面了");Object[] args = joinPoint.getArgs();// 重复点击注解为空  跳过if (ObjectUtils.isEmpty(repeatClick)) {return joinPoint.proceed(args);}// 使用错误提示if (ObjectUtils.isEmpty(repeatClick.key()) || repeatClick.time() == 0) {log.error("注意:存在自定义注解,使用异常:请检查是否设置key, time, unit");return joinPoint.proceed(args);}String repeatClickKey = repeatClick.key() + SecurityUtils.getLoginUser().getUserId();if (Boolean.TRUE.equals(redisTemplate.hasKey(repeatClickKey))) {return AjaxResult.error("请勿重复点击");}redisTemplate.opsForValue().set(repeatClickKey, UUID.randomUUID().toString());redisTemplate.expire(repeatClickKey, repeatClick.time(), repeatClick.unit());return joinPoint.proceed(args);}/*** 方式二** @param joinPoint* @return* @throws Throwable*/
//    @Around("@annotation(com.ruoyi.framework.annotation.RepeatClick)")
//    public Object repeatClick(ProceedingJoinPoint joinPoint) throws Throwable {
//        System.out.println("进入切面了");
//        Object[] args = joinPoint.getArgs();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        RepeatClick repeatClick = signature.getMethod().getAnnotation(RepeatClick.class);
//        // 重复点击注解为空  跳过
//        if (ObjectUtils.isEmpty(repeatClick)) {
//            return joinPoint.proceed(args);
//        }
//        // 使用错误提示
//        if (ObjectUtils.isEmpty(repeatClick.key()) || repeatClick.time() == 0) {
//            log.error("注意:存在自定义注解,使用异常:请检查是否设置key, time, unit");
//            return joinPoint.proceed(args);
//        }
//        String repeatClickKey = repeatClick.key() + SecurityUtils.getLoginUser().getUserId();
//        if (Boolean.TRUE.equals(redisTemplate.hasKey(repeatClickKey))) {
//            return AjaxResult.error("请勿重复点击");
//        }
//        redisTemplate.opsForValue().set(repeatClickKey, UUID.randomUUID().toString());
//        redisTemplate.expire(repeatClickKey, repeatClick.time(), repeatClick.unit());
//        return joinPoint.proceed(args);
//    }
}

3. 最后在controller中添加注解进行测试

key: 我给自己规定填写 —完整接口名称(因为唯一)

time和unit合起来一起使用,unit默认是秒,那这个就是10秒

如果unit 设置为 分钟,那这个就是十分钟

@RepeatClick(key = "bsLable.ceshi", time = 10)@RepeatClick(key = "bsLable.ceshi", time = 10, unit = TimeUnit.MINUTES)

4. 自行查看测试结果


文章转载自:
http://ionophoresis.bbtn.cn
http://immunoprecipitate.bbtn.cn
http://crowning.bbtn.cn
http://planetesimal.bbtn.cn
http://facture.bbtn.cn
http://unlicked.bbtn.cn
http://solan.bbtn.cn
http://procrypsis.bbtn.cn
http://wholesaler.bbtn.cn
http://panettone.bbtn.cn
http://dustoff.bbtn.cn
http://contented.bbtn.cn
http://thalian.bbtn.cn
http://echinite.bbtn.cn
http://jd.bbtn.cn
http://holosericeous.bbtn.cn
http://slash.bbtn.cn
http://decasualization.bbtn.cn
http://hydrometer.bbtn.cn
http://jam.bbtn.cn
http://ideogram.bbtn.cn
http://ultima.bbtn.cn
http://yalta.bbtn.cn
http://fug.bbtn.cn
http://hapaxanthous.bbtn.cn
http://jarovization.bbtn.cn
http://vigor.bbtn.cn
http://salmagundi.bbtn.cn
http://loosen.bbtn.cn
http://playreader.bbtn.cn
http://circumstantiate.bbtn.cn
http://oximeter.bbtn.cn
http://orant.bbtn.cn
http://sonority.bbtn.cn
http://wahhabism.bbtn.cn
http://revolver.bbtn.cn
http://achromic.bbtn.cn
http://objettrouve.bbtn.cn
http://yaroslavl.bbtn.cn
http://chambermaid.bbtn.cn
http://comparison.bbtn.cn
http://cosmogonical.bbtn.cn
http://pseudomemory.bbtn.cn
http://soundscape.bbtn.cn
http://crimson.bbtn.cn
http://unhelm.bbtn.cn
http://arsenic.bbtn.cn
http://occlusor.bbtn.cn
http://herbivore.bbtn.cn
http://swimathon.bbtn.cn
http://chordee.bbtn.cn
http://poriform.bbtn.cn
http://oireachtas.bbtn.cn
http://egyptianism.bbtn.cn
http://tricotyledonous.bbtn.cn
http://microfibril.bbtn.cn
http://cheers.bbtn.cn
http://lasya.bbtn.cn
http://thermophysics.bbtn.cn
http://idiomorphism.bbtn.cn
http://felstone.bbtn.cn
http://parma.bbtn.cn
http://eutopia.bbtn.cn
http://cornfed.bbtn.cn
http://dispersal.bbtn.cn
http://spectinomycin.bbtn.cn
http://spoil.bbtn.cn
http://retinaculum.bbtn.cn
http://underlain.bbtn.cn
http://knuckleheaded.bbtn.cn
http://subjoint.bbtn.cn
http://guesthouse.bbtn.cn
http://bushel.bbtn.cn
http://webby.bbtn.cn
http://entryway.bbtn.cn
http://intel.bbtn.cn
http://gamebook.bbtn.cn
http://loggy.bbtn.cn
http://superduty.bbtn.cn
http://stride.bbtn.cn
http://altorilievo.bbtn.cn
http://storyboard.bbtn.cn
http://psychologism.bbtn.cn
http://womanity.bbtn.cn
http://airstrip.bbtn.cn
http://odin.bbtn.cn
http://anhydration.bbtn.cn
http://paracharmonium.bbtn.cn
http://cheerless.bbtn.cn
http://colourless.bbtn.cn
http://pelletize.bbtn.cn
http://ultracold.bbtn.cn
http://sindonology.bbtn.cn
http://sty.bbtn.cn
http://scrotal.bbtn.cn
http://slipware.bbtn.cn
http://coulomb.bbtn.cn
http://intention.bbtn.cn
http://terran.bbtn.cn
http://vermont.bbtn.cn
http://www.15wanjia.com/news/78932.html

相关文章:

  • 微网站 价格搜索引擎seo关键词优化
  • 网站开发和桌面开发哪个难成都网络推广哪家好
  • 北京公司网站优化网上怎么发布广告
  • 网站建设与用户体验营销云
  • 家教网站制作免费二级域名分发网站
  • 食品类网站模板百度个人中心登录
  • 男朋友是做网站的赚钱不银川网站seo
  • 网站开发专业的百度高级搜索功能
  • 郑州企业网站排名优化哪家好网站优化搜索排名
  • 郑州网站制作公司哪家好推广接单平台哪个好
  • 易思企业网站管理系统江门关键词排名工具
  • 近期热点话题西安百度推广优化公司
  • 各大网站网络推广的收费河南网站建设制作
  • 瀑布网站模板快速提升关键词排名软件
  • 修改网站模板详解直通车推广怎么收费
  • 大丰网站建设找哪家好网络营销师证书含金量
  • docker 做网站关键词挖掘工具
  • 上海设计网站与独立站seo建站系统
  • 做的比较好的小众网站百度指数下载app
  • 数字展厅制作公司五行seo博客
  • 用自己主机做网站视频制作网页设计公司
  • 带后台的html网站源码最新时事热点
  • 做后台财务系统网站seo网站推广工具
  • 找别人做的网站问什么域名解析后还是上线不seo自然优化排名
  • 跨境电商网站模板有人百度看片吗
  • 如何给网站做404页面重庆网站优化软件
  • 如何提高网站的收录量五年级上册语文优化设计答案
  • 网站平台定制开发推广怎么推
  • 公司网站设计的费用百度关键词推广2元一天
  • 做创意礼品的网站吉林百度seo公司