SpringBoot+Redis實現(xiàn)后端接口防重復(fù)提交校驗的示例
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請求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;
/**
* 過濾器(重寫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)識
@Value("${token.header}")
private String header;
@Autowired
private RedisService redisService;
/**
* 間隔時間,單位:秒
* <p>
* 兩次相同參數(shù)的請求,如果間隔時間大于該參數(shù),系統(tǒng)不會認(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ù)提交,請稍后再試");
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);
}
}
/**
* 驗證是否重復(fù)提交由子類實現(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());
// 請求地址(作為存放cache的key值)
String url = request.getRequestURI();
// 唯一值(沒有消息頭則使用請求地址)
String submitKey = request.getHeader(header);
if (StrUtil.isBlank(submitKey)) {
submitKey = url;
}
// 唯一標(biāo)識(指定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);
}
/**
* 判斷兩次間隔時間
*/
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ù)提交過濾器(重寫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傳參:


body傳參:


注:
(1)RedisService源碼請查看以下博客。
Spring Boot 配置Redis(緩存數(shù)據(jù)庫)實現(xiàn)保存、獲取、刪除數(shù)據(jù)
(2)只有加上@RepeatSubmit的接口才會進行防重復(fù)提交校驗。
到此這篇關(guān)于SpringBoot+Redis實現(xiàn)后端接口防重復(fù)提交校驗的示例的文章就介紹到這了,更多相關(guān)SpringBoot接口防重復(fù)提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解在Spring-Boot中實現(xiàn)通用Auth認(rèn)證的幾種方式
這篇文章主要介紹了詳解在Spring-Boot中實現(xiàn)通用Auth認(rèn)證的幾種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
SpringBoot將Spring fox更換為Springdoc的方法詳解
由于項目中使用Spring fox已經(jīng)不維護更新了,代碼掃描,掃出問題,需要將Spring fox更換為Spring Doc,所以本文給大家介紹了SpringBoot將Spring fox更換為Springdoc的方法,文中有相關(guān)的代碼供大家參考,需要的朋友可以參考下2024-01-01
Jenkins自動化部署SpringBoot項目的實現(xiàn)
本文主要介紹了Jenkins自動化部署SpringBoot項目的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-01-01
mybatis-plus?查詢傳入?yún)?shù)Map,返回List<Map>方式
這篇文章主要介紹了mybatis-plus?查詢傳入?yún)?shù)Map,返回List<Map>方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot Web詳解靜態(tài)資源規(guī)則與定制化處理
這篇文章主要介紹了SpringBoot web場景的靜態(tài)資源規(guī)則與定制化,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
SpringCloud組件OpenFeign之?dāng)r截器解讀
這篇文章主要介紹了SpringCloud組件OpenFeign之?dāng)r截器用法。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

