SpringBoot+Redis实现后端接⼝防重复提交校验的⽰例⽬录
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
构建可重复读取inputStream的request。
package com.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.nio.charset.Charset;
/
**
* 构建可重复读取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 {
springboot和过滤器final ByteArrayInputStream bais = new Bytes("UTF-8"));
return new ServletInputStream() {
@Override
public int read() throws IOException {
ad();
}
@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内容
*
* @param request
* @return
*/
private String getBodyString(ServletRequest request) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try (InputStream inputStream = InputStream()) {
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); String line = "";
while ((line = adLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String();
}
}
3 RepeatableFilter
将原⽣的request变为可重复读取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变为可重复读取inputStream的request
if (request instanceof HttpServletRequest
&& StringUtils.ContentType(), MediaType.APPLICATION_JSON_VALUE)) {
requestWrapper = new RepeatedlyRequestWrapper((HttpServletRequest) request, response);
}
if (null == requestWrapper) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
}
4 RepeatSubmit
⾃定义注解(防⽌表单重复提交)。
package com.annotation;
import java.lang.annotation.*;
/**
* ⾃定义注解防⽌表单重复提交
*/
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit {
}
5 RepeatSubmitInterceptor
防⽌重复提交。
package com.interceptor;
import 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.hod.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.*;
import java.io.IOException;
import flect.Method;
import java.util.*;
import urrent.TimeUnit;
/**
* 防⽌重复提交
*/
@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:";
// 令牌⾃定义标识
@Value("${token.header}")
private String header;
@Autowired
private RedisService redisService;
/**
* 间隔时间,单位:秒
* <p>
* 两次相同参数的请求,如果间隔时间⼤于该参数,系统不会认定为重复提交的数据
*/
@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 = Method();
RepeatSubmit annotation = Annotation(RepeatSubmit.class);
if (annotation != null) {
if (this.isRepeatSubmit(request)) {
//返回重复提交提⽰
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("code", "500");
resultMap.put("msg", RequestURI() + "不允许重复提交,请稍后再试");
try {
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
return true;
} else {
return preHandle(request, response, handler);
}
}
/**
* 验证是否重复提交由⼦类实现具体的防重复提交的规则
*
* @param request
* @return
* @throws Exception
*/
public boolean isRepeatSubmit(HttpServletRequest request) {
String nowParams = "";
if (request instanceof RepeatedlyRequestWrapper) {
RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
nowParams = Body();
}
// body参数为空,获取Parameter的数据
if (StrUtil.isBlank(nowParams)) {
nowParams = ParameterMap());
}
Map<String, Object> nowDataMap = new HashMap<String, Object>();
nowDataMap.put(REPEAT_PARAMS, nowParams);
nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());
/
/ 请求地址(作为存放cache的key值)
String url = RequestURI();
// 唯⼀值(没有消息头则使⽤请求地址)
String submitKey = Header(header);
if (StrUtil.isBlank(submitKey)) {
submitKey = url;
}
// 唯⼀标识(指定key + 消息头)
String cacheRepeatKey = REPEAT_SUBMIT_KEY + submitKey;
Object sessionObj = CacheObject(cacheRepeatKey);
if (sessionObj != null) {
Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
if (ainsKey(url)) {
Map<String, Object> preDataMap = (Map<String, Object>) (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;
}
/**
* 判断参数是否相同
*/
private boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap) {
String nowParams = (String) (REPEAT_PARAMS);
String preParams = (String) (REPEAT_PARAMS);
return nowParams.equals(preParams);
}
/**
* 判断两次间隔时间
*/
private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap) {
long time1 = (Long) (REPEAT_TIME);
long time2 = (Long) (REPEAT_TIME);
if ((time1 - time2) < (this.intervalTime * 1000)) {
return true;
}
return false;
}
}
6 RepeatSubmitConfig
防重复提交配置。
fig;
import com.filter.RepeatableFilter;
import com.interceptor.RepeatSubmitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import t.annotation.*;
import org.springframework.fig.annotation.*;
/**
* 防重复提交配置
*/
@Configuration
public class RepeatSubmitConfig implements WebMvcConfigurer {
@Autowired
private RepeatSubmitInterceptor repeatSubmitInterceptor;
/**
* 添加防重复提交拦截
*/
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
}
/**
* ⽣成防重复提交过滤器(重写request)
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FilterRegistrationBean createRepeatableFilter()
{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论