最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java后端如何實(shí)現(xiàn)防止接口重復(fù)提交

 更新時(shí)間:2024年05月30日 10:20:56   作者:是小故事呀  
這篇文章主要介紹了java后端如何實(shí)現(xiàn)防止接口重復(fù)提交問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java后端防止接口重復(fù)提交

利用redis實(shí)現(xiàn)防止前端重復(fù)點(diǎn)擊按鈕重復(fù)提交。

解釋:

此方法是利用AOP+Redis實(shí)現(xiàn)防止接口重復(fù)請(qǐng)求

在需要防止重復(fù)提交的方法上,添加CheckRepeatCommit 注解。

此注解額,有兩個(gè)屬性,channel屬性是當(dāng)前訪問(wèn)的系統(tǒng),redis中key的前綴。

可不填。

expireTime 屬性,是添加了此注解的方法多久之內(nèi)不允許同一用戶重復(fù)請(qǐng)求,默認(rèn)3秒。

防止重復(fù)提交的原理就是:

在每次請(qǐng)求加了CheckRepeatCommit注解的接口時(shí),都會(huì)利用AOP在redis中保存一個(gè)從1開(kāi)始的自增數(shù)字,并設(shè)置此KEY的過(guò)期時(shí)間,當(dāng)后續(xù)同一個(gè)用戶再次請(qǐng)求時(shí),判斷此自增數(shù)字是否已存在并>=1,如果已存在并>=1,拋出異常。

話不多說(shuō),直接上代碼

自定義注解

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckRepeatCommit {

    String channel() default "APP";

    int expireTime() default 3;
}

定義切面

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

@Component
@Aspect
@Slf4j
public class CheckRepeatCommitAspect {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Pointcut("@annotation(com.upbim.twin.park.common.annotation.CheckRepeatCommit)")
    private void checkRepeatCommit() {

    }

    @Around("checkRepeatCommit()")
    public Object checkRepeatCommit(ProceedingJoinPoint joinPoint) throws Throwable {

        String methodName = joinPoint.getSignature().getName();
        Object target = joinPoint.getTarget();
        //得到攔截的方法
        Method method = getMethodByClassAndName(target.getClass(), methodName);
        log.info("驗(yàn)證是否重復(fù)提交:" + "調(diào)用方法:" + method);

        //請(qǐng)求的方法名
        String className = joinPoint.getTarget().getClass().getName();

        String channel = "";
        String bizKey = method.getName();
        int expireTime = 0;

        // 獲取當(dāng)前請(qǐng)求方法的注解,根據(jù)注解配置獲取參數(shù)
        CheckRepeatCommit checkRepeatCommit = method.getAnnotation(CheckRepeatCommit.class);

        String userNo = SysUserContextHolder.getSysUser().getUserNo();
        String key;

        if (checkRepeatCommit != null) {
            //注解上的描述
            channel = checkRepeatCommit.channel();
            expireTime = checkRepeatCommit.expireTime();

            key = getRepeatCommitLock(channel, className, bizKey, userNo, expireTime);

            if (StringUtils.isBlank(key)) {
                throw new SystemException(ResultEnum.RE_COMMIT);
            }
        }
        return joinPoint.proceed();
    }

    /**
     * 根據(jù)類和方法名得到方法
     */
    public Method getMethodByClassAndName(Class c, String methodName) {
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        return null;
    }

    public String getRepeatCommitLock(String channel, String module, String bizKey, String userNo, int expireTime) {

        if (StringUtils.isEmpty(module) || StringUtils.isEmpty(bizKey)) {
            throw new ParamException(ResultEnum.PARAMETER_CHECK_ERROR, "getRepeatCommitLock{} 參數(shù)不能為空!");
        }

        String redisKey = channel + StrUtil.COLON + module + StrUtil.COLON + bizKey + StrUtil.COLON + userNo;

        long count = redisTemplate.opsForValue().increment(redisKey, 1);

        if (count == 1) {
            if (expireTime == 0) {
                expireTime = 60;
            }
            redisTemplate.expire(redisKey, expireTime, TimeUnit.SECONDS);
            return redisKey;
        } else {
            return null;
        }
    }

}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

屏山县| 徐汇区| 华宁县| 诸暨市| 黔西县| 苍梧县| 安吉县| 广昌县| 大关县| 环江| 凤山县| 威信县| 灵武市| 惠东县| 许昌县| 应城市| 池州市| 阳朔县| 清新县| 灵山县| 松滋市| 陵川县| 双辽市| 鸡泽县| 郸城县| 盘锦市| 鄂托克旗| 霍州市| 承德县| 鹤壁市| 图木舒克市| 东兰县| 正蓝旗| 兴山县| 宣汉县| 连山| 德钦县| 六安市| 安乡县| 金塔县| 安化县|