Java?限制前端重復(fù)請(qǐng)求的實(shí)例代碼
背景及用途
前端頁(yè)面出現(xiàn)卡頓,用戶反復(fù)點(diǎn)擊操作按鈕,導(dǎo)致后臺(tái)接口短時(shí)間內(nèi)多次提交
實(shí)現(xiàn)步驟
設(shè)置切面,增加注解,導(dǎo)致在規(guī)定時(shí)間內(nèi)該接口不可重復(fù)調(diào)用
設(shè)置一個(gè)接口 NoRepeatSubmit
import java.lang.annotation.*; /** * xzj_2022_8_2 * 重復(fù)請(qǐng)求限制切面 */ @Target(ElementType.METHOD) //注解放置的目標(biāo)位置,METHOD是可注解在方法級(jí)別上 @Retention(RetentionPolicy.RUNTIME) //注解在哪個(gè)階段執(zhí)行 @Documented //生成文檔 public @interface NoRepeatSubmit { String name() default "name:"; }
實(shí)現(xiàn)類
import java.lang.annotation.*; /** * xzj_2022_8_2 * 重復(fù)請(qǐng)求限制切面 */ @Target(ElementType.METHOD) //注解放置的目標(biāo)位置,METHOD是可注解在方法級(jí)別上 @Retention(RetentionPolicy.RUNTIME) //注解在哪個(gè)階段執(zhí)行 @Documented //生成文檔 public @interface NoRepeatSubmit { String name() default "name:"; }
使用
@GetMapping(value = "/test") @NoRepeatSubmit public void test() { System.out.println("test"); }
補(bǔ)充:下面看下java防止前端重復(fù)提交
JAVA利用自定義本地鎖解決重復(fù)提交的問題
1.引入jar包
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>31.1-jre</version> </dependency>
2.自定義本地鎖
package com.hzt.listener; import java.lang.annotation.*; /** * 自定義-控制重復(fù)提交鎖 */ @Target(ElementType.METHOD) //作用于方法 @Retention(RetentionPolicy.RUNTIME) //運(yùn)行時(shí)有效 @Documented @Inherited public @interface LocalLock { String key() default ""; }
3.自定義注解切面 (aop攔截器實(shí)現(xiàn))
import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; /** * @Desc: 自定義注解攔截器 * @Author: zmk * @Date: 2022/4/2 */ @Aspect @Configuration public class LockMethodInterceptor { private final Logger log = LoggerFactory.getLogger(LockMethodInterceptor.class); private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder() //最大緩存數(shù) .maximumSize(1000) //設(shè)置過期時(shí)間 .expireAfterWrite(3, TimeUnit.SECONDS) .build(); @Around(value = "@annotation(localLock)") public Object interceptor (ProceedingJoinPoint point, LocalLock localLock) { //localLock.key() 這個(gè)是獲取controller的key屬性, point.getArgs()獲取key的值 String key = getKey(localLock.key(), point.getArgs()); if (StringUtils.isNotBlank(key)) { if (CACHES.getIfPresent(key) != null) { throw new RuntimeException("請(qǐng)勿重復(fù)提交"); } //如果是第一次請(qǐng)求, 將key放入緩存 CACHES.put(key, key); } try { return point.proceed(); } catch (Throwable throwable) { throw new RuntimeException("服務(wù)器異常"); } finally { //標(biāo)記為無(wú)效 // CACHES.invalidate(key); } } /** * * key 生成策略 * @param key key表達(dá)式 * @param args 參數(shù) * @return 生成的key */ private String getKey(String key, Object[] args) { for (int i = 0; i < args.length; i++) { key = key.replace("arg[" + i + "]", args[i].toString()); } return key; }
4.定義controller接口
@GetMapping("/query") @LocalLock(key = "param:arg[0]") public String query (@RequestParam("abc") String abc) { return "ok"; }
第一次調(diào)用結(jié)果:
第二次調(diào)用結(jié)果:
到此這篇關(guān)于Java 限制前端重復(fù)請(qǐng)求的文章就介紹到這了,更多相關(guān)Java 限制重復(fù)請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java BigDecimal使用及基本運(yùn)算(推薦)
Java在java.math包中提供的API類BigDecimal,用來(lái)對(duì)超過16位有效位的數(shù)進(jìn)行精確的運(yùn)算。這篇文章主要介紹了Java BigDecimal使用指南針(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Mybatis useGeneratedKeys參數(shù)用法及問題小結(jié)
這篇文章主要介紹了Mybatis useGeneratedKeys參數(shù)用法及遇到的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05springboot使用外置tomcat啟動(dòng)方式
這篇文章主要介紹了springboot使用外置tomcat啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Python機(jī)器學(xué)習(xí)三大件之二pandas
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)三大件之二pandas,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05Java8中forEach語(yǔ)句循環(huán)一個(gè)List和Map
這篇文章主要給大家介紹了關(guān)于Java8中forEach語(yǔ)句循環(huán)一個(gè)List和Map的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02