springmvc防止重復提交攔截器

一、攔截器實現,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;/*** 重復請求阻止攔截器*/
@Component("resubmitInterceptorHandler")
public class ResubmitInterceptorHandler extends HandlerInterceptorAdapter {private RedisUtils redisUtils;//自定義public ResubmitInterceptorHandler(RedisUtils redisUtils) {this.redisUtils = redisUtils;}/*** 攔截重復提交的請求** @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();// 獲取重復提交的鍵值String key = getKey(request, method);String value = redisUtils.get(key, String.class);if (StringUtils.isBlank(value)) {// 如果存在就存儲到redis中
                redisUtils.set(key, seconds.toString(), seconds);return true;} else {throw new Exception("請不要在" + seconds + "秒內重復請求");}}}/*** 獲取redis存儲的鍵** @param request* @param method* @return*/private String getKey(HttpServletRequest request, HandlerMethod method) {StringBuffer sb = new StringBuffer();String requestURI = request.getRequestURI();// 拼接請求路徑
        sb.append(requestURI);Method targetMethod = method.getMethod();// 拼接目標方法名稱
        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;/*** 指定多少時間以內不能重復提交* -1 表示不進行處理** @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);}}

?

轉載于:https://www.cnblogs.com/hujunzheng/p/6780371.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/531263.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/531263.shtml
英文地址,請注明出處:http://en.pswp.cn/news/531263.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Mac idea 快捷鍵

Mac鍵盤符號和修飾鍵說明 ? Command? Shift? Option? Control?? Return/Enter? Delete? 向前刪除鍵&#xff08;FnDelete&#xff09;↑ 上箭頭↓ 下箭頭← 左箭頭→ 右箭頭? Page Up&#xff08;Fn↑&#xff09;? Page Down&#xff08;Fn↓&#xff09;Home Fn ←…

cas4.2.7實現單點登錄

準備前參考&#xff1a;  cas server下載地址 cas client 下載地址 安全cookie setSecure詳解 Spring通過構造方法注入的四種方式 cas 學習博文 自定義登錄頁和登錄認證 cas server端的login-webflow詳細流程 CAS服務端自定義數據庫認證用戶 準備工作 1. cas server下載之后解…

log4j之log4j2.xml使用

依賴jar包 log4j-api-2.6.2.jar log4j-core-2.6.2.jar log4j-slf4j-impl-2.6.2.jar slf4j-api-1.7.12.jar 在resources目錄下新建log4j2.xml&#xff0c;內容如下。 <?xml version"1.0" encoding"UTF-8"?><!--status : 這個用于設置log4j2自身內…

cas4.2.7與shiro進行整合

準備工作 cas單點登錄開始前準備&#xff0c;請參考cas4.2.7實現單點登錄。 與shiro進行整合 注&#xff1a;準備工作的基礎上&#xff0c;對cas客戶端進行如下改進。 引入相關jar包 shiro-cas-1.2.6.jar shiro-core-1.2.6.jar shiro-spring-1.2.6.jar shiro-web-1.2.6.jar web…

命令行fuck神器

文章 thefuck git thefuck 轉載于:https://www.cnblogs.com/hujunzheng/p/6935587.html

springmvc配置MappingJackson2HttpMessageConverter實現屬性駝峰和下劃線的轉換

需求 php調用java接口時&#xff0c;因為php那邊的屬性都是下劃線風格&#xff0c;java這邊的屬性都是駝峰的風格。配置springmvc的json轉換&#xff0c;在requestBody的時候&#xff08;調用對象的set 方法&#xff09;將java屬性name映射成下劃線形式 和 請求的參數匹配&…

springmvc中使用MockMvc測試controller

示例代碼 import com.alibaba.fastjson.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springfra…

swagger restful api form映射實體對象和body映射實體對象配置

實體Model ModelAttribute一個具有如下三個作用&#xff1a; ①綁定請求參數到命令對象&#xff1a;放在功能處理方法的入參上時&#xff0c;用于將多個請求參數綁定到一個命令對象&#xff0c;從而簡化綁定流程&#xff0c;而且自動暴露為模型數據用于視圖頁面展示時使用&…

程序員的開發文檔

Web版&#xff1a; DevDocs API Documentation 桌面版&#xff1a;devdocs-app 轉載于:https://www.cnblogs.com/hujunzheng/p/7015947.html

ssh端口轉發(之kettle ssh方式連接數據庫)

ssh參數解釋 格式  ssh [user]host [command] 選項&#xff1a; -1&#xff1a;強制使用ssh協議版本1&#xff1b; -2&#xff1a;強制使用ssh協議版本2&#xff1b; -4&#xff1a;強制使用IPv4地址&#xff1b; -6&#xff1a;強制使用IPv6地址&#xff1b; -A&#xff1a…

ThreadLocal和InheritableThreadLocal使用

InheritableThreadLocal代碼 public class InheritableThreadLocal<T> extends ThreadLocal<T> {protected T childValue(T parentValue) {return parentValue;}ThreadLocalMap getMap(Thread t) {return t.inheritableThreadLocals;}void createMap(Thread t, T f…

mybatis generator修改默認生成的sql模板

相關連接&#xff1a; mybatis-generator擴展教程系列 -- 自定義sql xml文件 git項目地址 轉載于:https://www.cnblogs.com/hujunzheng/p/7110510.html

oauth簡單使用

一、oauth原理參考 理解OAuth 2.0 二、本例中采用授權碼模式 大致流程 &#xff08;A&#xff09;用戶訪問客戶端&#xff0c;后者將前者導向認證服務器。  &#xff08;B&#xff09;用戶選擇是否給予客戶端授權。  &#xff08;C&#xff09;假設用戶給予授權&#xff0c…

我眼中的服務提供和服務消費

服務提供和消費腦圖 服務提供和消費腦圖 參見: 服務提供者, 服務消費者, 服務注冊中心 服務提供者 1.服務提供者啟動&#xff0c;解析xml文件中配置的服務&#xff0c;這里使用Dom4j解析。 2.將服務的一些相關信息注冊到 服務注冊中心。 注&#xff1a;服務相關信息&#xff1a…

shiro整合oauth

前言 如果oauth原理還不清楚的地方&#xff0c;其參考這里。 一、基本思路腦圖 二、客戶端shiro配置 shiro配置文件 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans"xmlns:util&q…

springmvc+swagger2

一、swagger2依賴 <!--swagger--> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><exclusions><exclusion><artifactId>spring-aop</artifactId><groupId>org.s…

獲取資源文件工具類

如果沒有依賴spring&#xff0c;可以將分割線下的方法去掉 import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframe…

無狀態shiro認證組件(禁用默認session)

準備內容 簡單的shiro無狀態認證 無狀態認證攔截器 import com.hjzgg.stateless.shiroSimpleWeb.Constants; import com.hjzgg.stateless.shiroSimpleWeb.realm.StatelessToken; import org.apache.shiro.web.filter.AccessControlFilter;import javax.servlet.ServletRequest;…

Spring根據包名獲取包路徑下的所有類

參考mybatis MapperScannerConfigurer.java 最終找到 Spring的一個類 ClassPathBeanDefinitionScanner.java 參考ClassPathBeanDefinitionScanner 和它的父類 ClassPathScanningCandidateComponentProvider&#xff0c;將一些代碼進行抽取&#xff0c;得到如下工具類。 import…

java8 Optional正確使用姿勢

Java 8 如何正確使用 Optional import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; import org.apache.commons.lang3.StringUtils;import java.util.Optional;Data EqualsAndHashCode(exclude{"self"}) ToString(callSupertrue, exclud…