Spring Boot 使用 AOP 防止重復(fù)提交

在傳統(tǒng)的web項(xiàng)目中,防止重復(fù)提交,通常做法是:后端生成一個唯一的提交令牌(uuid),并存儲在服務(wù)端。頁面提交請求攜帶這個提交令牌,后端驗(yàn)證并在第一次驗(yàn)證后刪除該令牌,保證提交請求的唯一性。

上述的思路其實(shí)沒有問題的,但是需要前后端都稍加改動,如果在業(yè)務(wù)開發(fā)完在加這個的話,改動量未免有些大了,本節(jié)的實(shí)現(xiàn)方案無需前端配合,純后端處理。

思路

  1. 自定義注解 @NoRepeatSubmit 標(biāo)記所有Controller中的提交請求
  2. 通過AOP 對所有標(biāo)記了 @NoRepeatSubmit 的方法攔截
  3. 在業(yè)務(wù)方法執(zhí)行前,獲取當(dāng)前用戶的 token(或者JSessionId)+ 當(dāng)前請求地址,作為一個唯一 KEY,去獲取 Redis 分布式鎖(如果此時并發(fā)獲取,只有一個線程會成功獲取鎖)
  4. 業(yè)務(wù)方法執(zhí)行后,釋放鎖

關(guān)于Redis 分布式鎖

Code

這里只貼出 AOP 類和測試類,完整代碼見 ==> Gitee

@Aspect
@Component
public class RepeatSubmitAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(RepeatSubmitAspect.class);

    @Autowired
    private RedisLock redisLock;

    @Pointcut("@annotation(com.gitee.taven.aop.NoRepeatSubmit)")
    public void pointCut() {}

    @Around("pointCut()")
    public Object before(ProceedingJoinPoint pjp) {
        try {
            HttpServletRequest request = RequestUtils.getRequest();
            Assert.notNull(request, "request can not null");

            // 此處可以用token或者JSessionId
            String token = request.getHeader("Authorization");
            String path = request.getServletPath();
            String key = getKey(token, path);
            String clientId = getClientId();

            boolean isSuccess = redisLock.tryLock(key, clientId, 10);
            LOGGER.info("tryLock key = [{}], clientId = [{}]", key, clientId);

            if (isSuccess) {
                LOGGER.info("tryLock success, key = [{}], clientId = [{}]", key, clientId);
                // 獲取鎖成功, 執(zhí)行進(jìn)程
                Object result = pjp.proceed();
                // 解鎖
                redisLock.releaseLock(key, clientId);
                LOGGER.info("releaseLock success, key = [{}], clientId = [{}]", key, clientId);
                return result;

            } else {
                // 獲取鎖失敗,認(rèn)為是重復(fù)提交的請求
                LOGGER.info("tryLock fail, key = [{}]", key);
                return new ApiResult(200, "重復(fù)請求,請稍后再試", null);
            }

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return new ApiResult(500, "系統(tǒng)異常", null);
    }

    private String getKey(String token, String path) {
        return token + path;
    }

    private String getClientId() {
        return UUID.randomUUID().toString();
    }

}

多線程測試

測試代碼如下,模擬十個請求并發(fā)同時提交

@Component
public class RunTest implements ApplicationRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(RunTest.class);

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("執(zhí)行多線程測試");
        String url="http://localhost:8000/submit";
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for(int i=0; i<10; i++){
            String userId = "userId" + i;
            HttpEntity request = buildRequest(userId);
            executorService.submit(() -> {
                try {
                    countDownLatch.await();
                    System.out.println("Thread:"+Thread.currentThread().getName()+", time:"+System.currentTimeMillis());
                    ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
                    System.out.println("Thread:"+Thread.currentThread().getName() + "," + response.getBody());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        countDownLatch.countDown();
    }

    private HttpEntity buildRequest(String userId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "yourToken");
        Map<String, Object> body = new HashMap<>();
        body.put("userId", userId);
        return new HttpEntity<>(body, headers);
    }

}

成功防止重復(fù)提交,控制臺日志如下,可以看到十個線程的啟動時間幾乎同時發(fā)起,只有一個請求提交成功了

image

本節(jié)demo

戳這里 ==> Gitee
build項(xiàng)目之后,啟動本地redis,運(yùn)行項(xiàng)目自動執(zhí)行測試方法

參考

http://www.itdecent.cn/p/09c6b05b670a

作者:殷天文
鏈接:http://www.itdecent.cn/p/09860b74658e
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容