Java?限制前端重復(fù)請求的實例代碼
背景及用途
前端頁面出現(xiàn)卡頓,用戶反復(fù)點擊操作按鈕,導(dǎo)致后臺接口短時間內(nèi)多次提交
實現(xiàn)步驟
設(shè)置切面,增加注解,導(dǎo)致在規(guī)定時間內(nèi)該接口不可重復(fù)調(diào)用
設(shè)置一個接口 NoRepeatSubmit
import java.lang.annotation.*;
/**
* xzj_2022_8_2
* 重復(fù)請求限制切面
*/
@Target(ElementType.METHOD) //注解放置的目標(biāo)位置,METHOD是可注解在方法級別上
@Retention(RetentionPolicy.RUNTIME) //注解在哪個階段執(zhí)行
@Documented //生成文檔
public @interface NoRepeatSubmit {
String name() default "name:";
}實現(xiàn)類
import java.lang.annotation.*;
/**
* xzj_2022_8_2
* 重復(fù)請求限制切面
*/
@Target(ElementType.METHOD) //注解放置的目標(biāo)位置,METHOD是可注解在方法級別上
@Retention(RetentionPolicy.RUNTIME) //注解在哪個階段執(zhí)行
@Documented //生成文檔
public @interface NoRepeatSubmit {
String name() default "name:";
}使用
@GetMapping(value = "/test")
@NoRepeatSubmit
public void test() {
System.out.println("test");
}補充:下面看下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) //運行時有效
@Documented
@Inherited
public @interface LocalLock {
String key() default "";
}3.自定義注解切面 (aop攔截器實現(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è)置過期時間
.expireAfterWrite(3, TimeUnit.SECONDS)
.build();
@Around(value = "@annotation(localLock)")
public Object interceptor (ProceedingJoinPoint point, LocalLock localLock) {
//localLock.key() 這個是獲取controller的key屬性, point.getArgs()獲取key的值
String key = getKey(localLock.key(), point.getArgs());
if (StringUtils.isNotBlank(key)) {
if (CACHES.getIfPresent(key) != null) {
throw new RuntimeException("請勿重復(fù)提交");
}
//如果是第一次請求, 將key放入緩存
CACHES.put(key, key);
}
try {
return point.proceed();
} catch (Throwable throwable) {
throw new RuntimeException("服務(wù)器異常");
} finally {
//標(biāo)記為無效
// 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ù)請求的文章就介紹到這了,更多相關(guān)Java 限制重復(fù)請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis useGeneratedKeys參數(shù)用法及問題小結(jié)
這篇文章主要介紹了Mybatis useGeneratedKeys參數(shù)用法及遇到的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Python機器學(xué)習(xí)三大件之二pandas
這篇文章主要介紹了Python機器學(xué)習(xí)三大件之二pandas,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)Python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
Java8中forEach語句循環(huán)一個List和Map
這篇文章主要給大家介紹了關(guān)于Java8中forEach語句循環(huán)一個List和Map的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

