package com.gkhy.safePlatform.safeCheck.aspect;
|
|
|
import com.gkhy.safePlatform.safeCheck.annotation.RepeatedClick;
|
import com.gkhy.safePlatform.safeCheck.exception.RepeatedClickException;
|
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.util.concurrent.TimeUnit;
|
|
@Component
|
@Aspect
|
public class RepeatedClickAspect {
|
|
@Autowired
|
private RedisTemplate redisTemplate;
|
|
@Before("@annotation(com.gkhy.safePlatform.safeCheck.annotation.RepeatedClick)")
|
@ResponseBody
|
public void beforeRepeatedClick(JoinPoint joinPoint){
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
HttpServletRequest arg = requestAttributes.getRequest();
|
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
RepeatedClick annotation = methodSignature.getMethod().getAnnotation(RepeatedClick.class);
|
if (annotation != null){
|
int clickTime = annotation.clickTime();
|
String errorMessage = annotation.errorMessage();
|
String key = arg.getRemoteAddr() + arg.getRequestURI() + "_" + arg.getMethod();
|
if (redisTemplate.hasKey(key)){
|
throw new RepeatedClickException(errorMessage);
|
}else {
|
redisTemplate.opsForValue().set(key,"",clickTime, TimeUnit.SECONDS);
|
}
|
}
|
}
|
}
|