updateInstallBot接口添加重复请求限制

This commit is contained in:
wanjia 2023-04-12 17:17:12 +08:00
parent edc3830b5a
commit 06cadbe055
5 changed files with 105 additions and 0 deletions

View File

@ -149,6 +149,12 @@
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,14 @@
package com.gitlink.softbot.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD) // 作用到方法上
@Retention(RetentionPolicy.RUNTIME) // 运行时有效
public @interface NoRepeatSubmit {
}

View File

@ -0,0 +1,21 @@
package com.gitlink.softbot.annotation;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description: 内存缓存配置类
*/
@Configuration
public class UrlCache {
@Bean
public Cache<String, Integer> getCache() {
return CacheBuilder.newBuilder().expireAfterWrite(2L, TimeUnit.SECONDS).build();// 缓存有效期为2秒
}
}

View File

@ -0,0 +1,61 @@
package com.gitlink.softbot.annotation.aop;
import javax.servlet.http.HttpServletRequest;
import com.gitlink.softbot.annotation.NoRepeatSubmit;
import com.gitlink.softbot.global.exception.BotException;
import com.gitlink.softbot.global.vo.Result;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.google.common.cache.Cache;
/**
* @Description: aop解析注解-配合google的Cache缓存机制
* @Author: Zoutao
* @Date: 2020/4/14
*/
@Aspect
@Component
public class NoRepeatSubmitAop {
private Log logger = LogFactory.getLog(getClass());
@Autowired
private Cache<String, Integer> cache;
@Pointcut("@annotation(noRepeatSubmit)")
public void pointCut(NoRepeatSubmit noRepeatSubmit) {
}
@Around("pointCut(noRepeatSubmit)")
public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) {
Result<String> result = new Result<>();
try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
HttpServletRequest request = attributes.getRequest();
String key = sessionId + "-" + request.getServletPath();
if (cache.getIfPresent(key) == null) {// 如果缓存中有这个url视为重复提交
Object o = pjp.proceed();
cache.put(key, 0);
return o;
} else {
logger.error("重复请求,请稍后再试!");
return result.build(500).build("重复请求,请稍后再试!");
}
} catch (Throwable e) {
e.printStackTrace();
logger.error("验证重复提交时出现未知异常!");
return result.build(500).build("验证重复提交时出现未知异常!");
}
}
}

View File

@ -1,6 +1,7 @@
package com.gitlink.softbot.controller.user;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gitlink.softbot.annotation.NoRepeatSubmit;
import com.gitlink.softbot.global.exception.BotException;
import com.gitlink.softbot.global.vo.Response;
import com.gitlink.softbot.global.vo.Result;
@ -9,6 +10,7 @@ import com.gitlink.softbot.utils.AuthOperate;
import com.gitlink.softbot.utils.GitLinkApi;
import com.gitlink.softbot.vo.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.gitlink.softbot.vo.BotInputVO;
import javax.annotation.Resource;
@ -181,6 +183,7 @@ public class UserController {
* @return
*/
@PostMapping("/updateInstallBot")
@NoRepeatSubmit
public Result<?> updateInstallBot(@Valid @RequestBody UpdateInstallBotRequest updateInstallBotRequest) throws Exception{
Result<?> result = new Result<>();
userService.updateInstallBot(updateInstallBotRequest);