溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

發(fā)布時(shí)間:2021-06-23 13:48:28 來(lái)源:億速云 閱讀:483 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)”,在日常操作中,相信很多人在SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

目錄
  • 1 Maven依賴

  • 2 RepeatedlyRequestWrapper

  • 3 RepeatableFilter

  • 4 RepeatSubmit

  • 5 RepeatSubmitInterceptor

  • 6 RepeatSubmitConfig

  • 7 RepeatSubmitController

1 Maven依賴

<!--redis緩存-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
 
        <!-- 阿里JSON解析器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <!--hutool工具包-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.1</version>
        </dependency>

2 RepeatedlyRequestWrapper

構(gòu)建可重復(fù)讀取inputStream的request。

package com.servlet;
 
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.nio.charset.Charset;
 
/**
 * 構(gòu)建可重復(fù)讀取inputStream的request
 */
public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper {
    private final String body;
 
    public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException {
        super(request);
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
 
        body = getBodyString(request);
    }
 
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }
 
    public String getBody() {
        return body;
    }
 
    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream bais = new ByteArrayInputStream(body.getBytes("UTF-8"));
        return new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return bais.read();
            }
 
            @Override
            public int available() throws IOException {
                return body.length();
            }
 
            @Override
            public boolean isFinished() {
                return false;
            }
 
            @Override
            public boolean isReady() {
                return false;
            }
 
            @Override
            public void setReadListener(ReadListener readListener) {
 
            }
        };
    }
 
    /**
     * 獲取Request請(qǐng)求body內(nèi)容
     *
     * @param request
     * @return
     */
    private String getBodyString(ServletRequest request) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try (InputStream inputStream = request.getInputStream()) {
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}

3 RepeatableFilter

將原生的request變?yōu)榭芍貜?fù)讀取inputStream的request。

package com.filter;
 
import com.servlet.RepeatedlyRequestWrapper;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
 
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
/**
 * 過(guò)濾器(重寫(xiě)request)
 */
public class RepeatableFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        //將原生的request變?yōu)榭芍貜?fù)讀取inputStream的request
        if (request instanceof HttpServletRequest
                && StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE)) {
            requestWrapper = new RepeatedlyRequestWrapper((HttpServletRequest) request, response);
        }
        if (null == requestWrapper) {
            chain.doFilter(request, response);
        } else {
            chain.doFilter(requestWrapper, response);
        }
    }
}

4 RepeatSubmit

自定義注解(防止表單重復(fù)提交)。

package com.annotation;
 
import java.lang.annotation.*;
 
/**
 * 自定義注解防止表單重復(fù)提交
 */
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit {
}

5 RepeatSubmitInterceptor

防止重復(fù)提交攔截器。

package com.interceptor;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.annotation.RepeatSubmit;
import com.service.*;
import com.servlet.RepeatedlyRequestWrapper;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
 
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * 防止重復(fù)提交攔截器
 */
@Component
public class RepeatSubmitInterceptor implements HandlerInterceptor {
    public final String REPEAT_PARAMS = "repeatParams";
 
    public final String REPEAT_TIME = "repeatTime";
    /**
     * 防重提交 redis key
     */
    public final String REPEAT_SUBMIT_KEY = "repeat_submit:";
 
    // 令牌自定義標(biāo)識(shí)
    @Value("${token.header}")
    private String header;
 
    @Autowired
    private RedisService redisService;
 
    /**
     * 間隔時(shí)間,單位:秒
     * <p>
     * 兩次相同參數(shù)的請(qǐng)求,如果間隔時(shí)間大于該參數(shù),系統(tǒng)不會(huì)認(rèn)定為重復(fù)提交的數(shù)據(jù)
     */
    @Value("${repeatSubmit.intervalTime}")
    private int intervalTime;
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
            if (annotation != null) {
                if (this.isRepeatSubmit(request)) {
                    //返回重復(fù)提交提示
                    Map<String, Object> resultMap = new HashMap<>();
                    resultMap.put("code", "500");
                    resultMap.put("msg", request.getRequestURI() + "不允許重復(fù)提交,請(qǐng)稍后再試");
                    try {
                        response.setStatus(200);
                        response.setContentType("application/json");
                        response.setCharacterEncoding("utf-8");
                        response.getWriter().print(JSONObject.toJSONString(resultMap));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return false;
                }
            }
            return true;
        } else {
            return preHandle(request, response, handler);
        }
    }
 
    /**
     * 驗(yàn)證是否重復(fù)提交由子類(lèi)實(shí)現(xiàn)具體的防重復(fù)提交的規(guī)則
     *
     * @param request
     * @return
     * @throws Exception
     */
    public boolean isRepeatSubmit(HttpServletRequest request) {
        String nowParams = "";
        if (request instanceof RepeatedlyRequestWrapper) {
            RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
            nowParams = repeatedlyRequest.getBody();
        }
        // body參數(shù)為空,獲取Parameter的數(shù)據(jù)
        if (StrUtil.isBlank(nowParams)) {
            nowParams = JSONObject.toJSONString(request.getParameterMap());
        }
        Map<String, Object> nowDataMap = new HashMap<String, Object>();
        nowDataMap.put(REPEAT_PARAMS, nowParams);
        nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());
 
        // 請(qǐng)求地址(作為存放cache的key值)
        String url = request.getRequestURI();
 
        // 唯一值(沒(méi)有消息頭則使用請(qǐng)求地址)
        String submitKey = request.getHeader(header);
        if (StrUtil.isBlank(submitKey)) {
            submitKey = url;
        }
 
        // 唯一標(biāo)識(shí)(指定key + 消息頭)
        String cacheRepeatKey = REPEAT_SUBMIT_KEY + submitKey;
 
        Object sessionObj = redisService.getCacheObject(cacheRepeatKey);
        if (sessionObj != null) {
            Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
            if (sessionMap.containsKey(url)) {
                Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
                if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap)) {
                    return true;
                }
            }
        }
        Map<String, Object> cacheMap = new HashMap<String, Object>();
        cacheMap.put(url, nowDataMap);
        redisService.setCacheObject(cacheRepeatKey, cacheMap, intervalTime, TimeUnit.SECONDS);
        return false;
    }
 
    /**
     * 判斷參數(shù)是否相同
     */
    private boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap) {
        String nowParams = (String) nowMap.get(REPEAT_PARAMS);
        String preParams = (String) preMap.get(REPEAT_PARAMS);
        return nowParams.equals(preParams);
    }
 
    /**
     * 判斷兩次間隔時(shí)間
     */
    private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap) {
        long time1 = (Long) nowMap.get(REPEAT_TIME);
        long time2 = (Long) preMap.get(REPEAT_TIME);
        if ((time1 - time2) < (this.intervalTime * 1000)) {
            return true;
        }
        return false;
    }
 
 
}

6 RepeatSubmitConfig

防重復(fù)提交配置。

package com.config;
 
import com.filter.RepeatableFilter;
import com.interceptor.RepeatSubmitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.*;
 
/**
 * 防重復(fù)提交配置
 */
@Configuration
public class RepeatSubmitConfig implements WebMvcConfigurer {
    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
    /**
     * 添加防重復(fù)提交攔截
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
    }
 
    /**
     * 生成防重復(fù)提交過(guò)濾器(重寫(xiě)request)
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean createRepeatableFilter()
    {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RepeatableFilter());
        registration.addUrlPatterns("/*");
        registration.setName("repeatableFilter");
        registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
        return registration;
    }
}

7 RepeatSubmitController

調(diào)試代碼。

package com.controller;
 
import com.annotation.RepeatSubmit;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@RestController
@RequestMapping("/repeatSubmit")
public class RepeatSubmitController {
    /**
     * 保存Param
     * @param name
     * @return
     */
    @RepeatSubmit
    @PostMapping("/saveParam/{name}")
    public String saveParam(@PathVariable("name")String name){
        return "保存Param成功";
    }
    /**
     * 保存Param
     * @param name
     * @return
     */
    @RepeatSubmit
    @PostMapping("/saveBody")
    public String saveBody(@RequestBody List<String> name){
        return "保存Body成功";
    }
}

8 調(diào)試結(jié)果

param傳參:

SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

body傳參:

SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

注:

(1)RedisService源碼請(qǐng)查看以下博客。

Spring Boot 配置Redis(緩存數(shù)據(jù)庫(kù))實(shí)現(xiàn)保存、獲取、刪除數(shù)據(jù)

(2)只有加上@RepeatSubmit的接口才會(huì)進(jìn)行防重復(fù)提交校驗(yàn)。

到此,關(guān)于“SpringBoot+Redis怎么實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI