個人學習系列 - springboot防止重複提交

最近開發項目時候發現,有時候因爲網絡或者個人問題,會出現重複點擊提交按鈕的情況,這樣有可能會在數據庫生成兩條數據,造成數據混淆。今天來談一下如何解決這個問題。

搭建springboot項目

1. 選擇新建項目

image.png

2. 選擇Spring Initializr

在這裏插入圖片描述

3. 填寫相關信息

在這裏插入圖片描述

4. 選擇web依賴

image.png

5. 選擇項目位置

image.png

開始代碼書寫啦

1. pom.xml文件依賴添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>29.0-jre</version>
</dependency>

2. 定義一個異常類

/**
 * 返回信息
 *
 * @author zhouzhaodong
 */
public class RestMessage {

    private int code;

    private String message;

    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public RestMessage(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public RestMessage(int code, String message) {
        this.code = code;
        this.message = message;
    }

}

3. 定義一個接口

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 定義一個註解
 * @apiNote @Target(ElementType.METHOD) 作用到方法上
 * @apiNote @Retention(RetentionPolicy.RUNTIME) 只有運行時有效
 * @author zhouzhaodong
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {

}

4. 定義一個切面

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 自定義一個切面類,利用aspect實現切入所有方法
 *
 * @author zhouzhaodong
 */
@Aspect
@Configuration
public class NoRepeatSubmitAop {

    private final Log logger = LogFactory.getLog(getClass());

    /**
     * 重複提交判斷時間爲2s
     */
    private final Cache<String, Integer> cache = CacheBuilder.newBuilder().expireAfterWrite(2L, TimeUnit.SECONDS).build();

    @Around("execution(* xyz.zhouzhaodong..*Controller.*(..)) && @annotation(nrs)")
    public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            String sessionId = Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getSessionId();
            assert attributes != null;
            HttpServletRequest request = attributes.getRequest();
            String key = sessionId + "-" + request.getServletPath();
            // 如果緩存中有這個url視爲重複提交
            if (cache.getIfPresent(key) == null) {
                Object o = pjp.proceed();
                cache.put(key, 0);
                return o;
            } else {
                logger.error("重複提交");
                return new RestMessage(888, "請勿短時間內重複操作");
            }
        } catch (Throwable e) {
            e.printStackTrace();
            logger.error("驗證重複提交時出現未知異常!");
            return new RestMessage(889, "驗證重複提交時出現未知異常!");
        }

    }

}

5. 寫controller進行測試

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 測試
 * @author zhouzhaodong
 */
@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 添加防重複提交註解
     * @return
     */
    @NoRepeatSubmit
    @RequestMapping("/one")
    public RestMessage test(){
        return new RestMessage(0, "測試通過");
    }

}

第一次點擊返回正常信息:
image.png
快速點擊第二次會出現錯誤信息:
image.png

測試通過!

源代碼地址爲:

https://github.com/zhouzhaodong/springboot/tree/master/repeat-submission

個人博客地址:

http://www.zhouzhaodong.xyz/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章