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

Java實現(xiàn)冪等性校驗的示例代碼

 更新時間:2024年02月18日 11:25:40   作者:名一  
我們在做web應用的時候通常會遇到前端提交按鈕重復點擊的場景,在某些新增操作上就需要做冪等性限制來保證數(shù)據(jù)的可靠性,所以本文主要介紹了如何使用java?aop實現(xiàn)冪等性校驗,需要的可以參考下

我們在做web應用的時候通常會遇到前端提交按鈕重復點擊的場景,在某些新增操作上就需要做冪等性限制來保證數(shù)據(jù)的可靠性。下面來用java aop實現(xiàn)冪等性校驗。

一:首先我們需要一個自定義注解

package com.yuku.yuku_erp.annotation;

import java.lang.annotation.*;

/**
 * @author 名一
 * @ClassName IdempotentAnnotation
 * @description: 用來標記需要校驗冪等性的接口
 * @datetime 2024年 02月 03日 14:48
 * @version: 1.0
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IdempotentAnnotation {
    String idempotentType();
}

二:創(chuàng)建一個冪等校驗的切面類

package com.yuku.yuku_erp.aop;

import com.yuku.yuku_erp.annotation.IdempotentAnnotation;
import com.yuku.yuku_erp.constant.RedisKeyConstant;
import com.yuku.yuku_erp.exception.MyException;
import com.yuku.yuku_erp.utils.RedisShardedPoolUtil;
import com.yuku.yuku_erp.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
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.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author 名一
 * @ClassName CheckIdempotentAop
 * @description: 冪等性校驗
 * @datetime 2024年 02月 03日 14:59
 * @version: 1.0
 */
@Slf4j
@Aspect
@Component
public class CheckIdempotentAop {

    @Pointcut("execution(* com.yuku.yuku_erp.controller..*.*(..))")
    public void checkCut(){
    }

    @Before("checkCut()")
    public void checkIdempotent(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if (method.isAnnotationPresent(IdempotentAnnotation.class)){
            IdempotentAnnotation annotation = method.getAnnotation(IdempotentAnnotation.class);
            String idempotentType = annotation.idempotentType();
            String idempotentToken = TokenUtil.getRequest().getHeader("idempotentToken");
            String idemToken = idempotentType + idempotentToken;
            log.info("checkIdempotent idempotentType:{}, idempotentToken:{}", idempotentType, idempotentToken);

            Boolean flag = RedisShardedPoolUtil.sismember(RedisKeyConstant.IDEMPOTENT_TOKEN_LIST, idemToken);
            if (!flag){
                log.error("checkIdempotent error idempotentType:{}, idempotentToken:{}, flag:{}", idempotentType, idempotentToken, flag);
                throw new MyException("該接口已提交過,請不要重復提交");
            }
            RedisShardedPoolUtil.delSetByValue(RedisKeyConstant.IDEMPOTENT_TOKEN_LIST, idemToken);
            log.info("checkIdempotent idempotentType:{}, idempotentToken:{}, flag:{}", idempotentType, idempotentToken, flag);
        }
    }
}

三:在需要切面的接口上使用冪等校驗注解

@IdempotentAnnotation(idempotentType = "checkIdempotentToken")
    @GetMapping("/checkIdempotentToken")
    @ApiOperation(value = "校驗冪等性示例")
    public CommonResult<String> checkIdempotentToken(){
        return CommonResult.success();
    }

到此冪等校驗就完成了

四:方法補充

除了上文的方法,小編還為大家整理了其他實現(xiàn)冪等校驗的方法,希望對大家有所幫助

1.唯一標識符校驗

一種簡單的方式是使用唯一標識符來校驗請求的冪等性,每次請求時,客戶端生成一個唯一的標識符,并將其作為請求的一部分發(fā)送給服務器。服務器在接收到請求后先檢查該標識符是否已經(jīng)存在,如果存在則認為是重復請求,直接忽略;如果不存在,則執(zhí)行相應的業(yè)務邏輯,并將標識符保存到一個冪等性校驗表中

下面是一個使用 UUID 實現(xiàn)的示例代碼:

// 生成唯一標識符
String requestId = UUID.randomUUID().toString();

// 發(fā)送清求
HttpResponse response = httpClient.execute(request);

// 檢查響應結(jié)果
if (response.getStatusLine().getStatusCode() == 200){
    // 保存標識符到冪等性校驗表
    idempotentTable.put(requestId, true);
}

2. Token 校驗

另一種方式是使用 Token 來校驗請求的冪等性。服務器在第一次收到請求時,在響應中返回一個唯一的 Token 給客戶端,客戶端在下次請求時將該 Token 作為請求的一部分發(fā)送給服務器。服務器在接收到請求后,先檢查該 Token 是否有效,如果有效則執(zhí)行相應的業(yè)務邏輯,并將 Token 標記為已使用;如果無效則忽略該請求。

下面是一個使用 Token 實現(xiàn)的示例代碼:

// 發(fā)送第一次詩求,并獲 Token
HttpResponse response1 = httpClient.execute(request);
String token = response1.getFirstHeader("Token").getValue();

// 發(fā)送第二次詩求,并附 Token
request.addHeader("Token",token);
HttpResponse response2 = httpClient.execute(request);

// 檢查響應結(jié)果
if (response2.getStatusLine().getstatusCode() == 200) {
    // 標記 Token 為已使用
    tokenService.markAsUsed(token);
}

到此這篇關(guān)于Java實現(xiàn)冪等性校驗的示例代碼的文章就介紹到這了,更多相關(guān)Java冪等性校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

沁源县| 安仁县| 通河县| 开封市| 随州市| 进贤县| 临洮县| 红原县| 安平县| 法库县| 黑水县| 阿克| 河津市| 高邮市| 宁国市| 阿拉尔市| 石嘴山市| 临颍县| 句容市| 安陆市| 开远市| 辽源市| 汪清县| 卢湾区| 城固县| 通山县| 镇江市| 阿合奇县| 东港市| 石渠县| 无棣县| 潜山县| 博乐市| 安丘市| 米易县| 汤阴县| 曲靖市| 吉木萨尔县| 九龙县| 北京市| 乌拉特前旗|