Java實現(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); }
到此這篇關于Java實現(xiàn)冪等性校驗的示例代碼的文章就介紹到這了,更多相關Java冪等性校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot接受前臺參數(shù)的6種方式以及統(tǒng)一響應代碼示例
這篇文章主要給大家介紹了關于SpringBoot接受前臺參數(shù)的6種方式以及統(tǒng)一響應的相關資料,前端負責展示頁面和用戶交互,而后端則負責處理業(yè)務邏輯和數(shù)據(jù)存儲,在這種架構(gòu)下前端需要將用戶輸入的數(shù)據(jù)發(fā)送給后端進行處理,需要的朋友可以參考下2023-12-12Java中HttpServletResponse響應中文出現(xiàn)亂碼問題
這篇文章主要介紹了Java中HttpServletResponse響應中文出現(xiàn)亂碼問題的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06Spring Boot日志收集及鏈路追蹤實現(xiàn)示例
這篇文章主要為大家介紹了Spring Boot日志收集及鏈路追蹤實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12