日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

springmvc防止重复提交拦截器

發(fā)布時(shí)間:2025/3/8 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc防止重复提交拦截器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、攔截器實(shí)現(xiàn),ResubmitInterceptorHandler.java

import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Map; import java.util.Set;/*** 重復(fù)請(qǐng)求阻止攔截器*/ @Component("resubmitInterceptorHandler") public class ResubmitInterceptorHandler extends HandlerInterceptorAdapter {private RedisUtils redisUtils;//自定義public ResubmitInterceptorHandler(RedisUtils redisUtils) {this.redisUtils = redisUtils;}/*** 攔截重復(fù)提交的請(qǐng)求** @param request* @param response* @param handler* @return* @throws Exception*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {HandlerMethod method = (HandlerMethod) handler;Resubmit resubmit = method.getMethodAnnotation(Resubmit.class);if (resubmit == null) {return true;} else {Long seconds = resubmit.seconds();// 獲取重復(fù)提交的鍵值String key = getKey(request, method);String value = redisUtils.get(key, String.class);if (StringUtils.isBlank(value)) {// 如果存在就存儲(chǔ)到redis中 redisUtils.set(key, seconds.toString(), seconds);return true;} else {throw new Exception("請(qǐng)不要在" + seconds + "秒內(nèi)重復(fù)請(qǐng)求");}}}/*** 獲取redis存儲(chǔ)的鍵** @param request* @param method* @return*/private String getKey(HttpServletRequest request, HandlerMethod method) {StringBuffer sb = new StringBuffer();String requestURI = request.getRequestURI();// 拼接請(qǐng)求路徑 sb.append(requestURI);Method targetMethod = method.getMethod();// 拼接目標(biāo)方法名稱 sb.append(targetMethod.getName());Map<String, String[]> parameterMap = request.getParameterMap();if (parameterMap != null) {Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();if (entries != null) {for (Map.Entry<String, String[]> entry : entries) {sb.append(entry.getKey()).append(Arrays.toString(entry.getValue()));}}}return sb.toString();} }

二、controller上要添加的注解

import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Resubmit {long value() default 0;/*** 指定多少時(shí)間以內(nèi)不能重復(fù)提交* -1 表示不進(jìn)行處理** @return*/long seconds(); }

三、攔截器配置

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration @Import(com.bqmart.utils.RedisUtils.class) public class InterceptorConfig extends WebMvcConfigurerAdapter {@Autowiredprivate xxx.RedisUtils redisUtils;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(validateInterceptorHandler()).addPathPatterns("/**");}@Beanpublic com.bqmart.interceptor.ResubmitInterceptorHandler resubmitInterceptorHandler() {return new ResubmitInterceptorHandler(redisUtils);}}

?

轉(zhuǎn)載于:https://www.cnblogs.com/hujunzheng/p/6780371.html

總結(jié)

以上是生活随笔為你收集整理的springmvc防止重复提交拦截器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。