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

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

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

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

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

package com.yuku.yuku_erp.annotation;

import java.lang.annotation.*;

/**
 * @author 名一
 * @ClassName IdempotentAnnotation
 * @description: 用來標(biāo)記需要校驗冪等性的接口
 * @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("該接口已提交過,請不要重復(fù)提交");
            }
            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();
    }

到此冪等校驗就完成了

四:方法補(bǔ)充

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

1.唯一標(biāo)識符校驗

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

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

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

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

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

2. Token 校驗

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

下面是一個使用 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);

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

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

相關(guān)文章

最新評論

吐鲁番市| 双桥区| 栾川县| 晋州市| 安仁县| 海门市| 伊宁县| 洪江市| 黄大仙区| 花莲县| 类乌齐县| 池州市| 察雅县| 台州市| 巴东县| 肃北| 南召县| 徐州市| 福建省| 美姑县| 栾川县| 平遥县| 邹平县| 将乐县| 冀州市| 和平区| 普安县| 辰溪县| 梅州市| 鹰潭市| 浙江省| 柏乡县| 贡山| 正宁县| 延津县| 唐山市| 莱芜市| 平安县| 黑水县| 屏边| 靖边县|