| | |
| | | package cc.mrbird.febs.common.aspect;
|
| | |
|
| | | import cc.mrbird.febs.common.annotation.SubmitRepeat;
|
| | | import cc.mrbird.febs.common.contants.AppContants;
|
| | | import cc.mrbird.febs.common.exception.FebsException;
|
| | | import cc.mrbird.febs.common.utils.RedisUtils;
|
| | | import cc.mrbird.febs.modules.api.entity.MemberEntity;
|
| | | import cn.hutool.core.util.StrUtil;
|
| | | import cn.hutool.crypto.asymmetric.KeyType;
|
| | | import cn.hutool.crypto.asymmetric.RSA;
|
| | | import com.alibaba.fastjson.JSON;
|
| | | import lombok.extern.slf4j.Slf4j;
|
| | | import org.aspectj.lang.ProceedingJoinPoint;
|
| | | import org.aspectj.lang.annotation.*;
|
| | | import org.springframework.stereotype.Component;
|
| | | import org.springframework.web.context.request.RequestContextHolder;
|
| | | import org.springframework.web.context.request.ServletRequestAttributes;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | | @Slf4j
|
| | | @Aspect
|
| | | @Component
|
| | | public class SubmitRepeatAspect {
|
| | |
|
| | | @Resource
|
| | | private RedisUtils redisUtil;
|
| | |
|
| | | private String key;
|
| | |
|
| | | @Pointcut("@annotation(submitRepeat)")
|
| | | public void submitRepeatPointCut(SubmitRepeat submitRepeat) {
|
| | |
|
| | | }
|
| | |
|
| | | @Before("submitRepeatPointCut(submitRepeat)")
|
| | | public void before(SubmitRepeat submitRepeat) {
|
| | | }
|
| | |
|
| | | @Around("submitRepeatPointCut(submitRepeat)")
|
| | | public Object around(ProceedingJoinPoint joinPoint, SubmitRepeat submitRepeat) throws Throwable {
|
| | | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
| | | HttpServletRequest request = attributes.getRequest();
|
| | |
|
| | | String headerToken = request.getHeader("token");
|
| | | String token = resolveToken(headerToken);
|
| | | if(StrUtil.isEmpty(token)) {
|
| | | throw new FebsException("系统繁忙");
|
| | | }
|
| | | String object = (String)redisUtil.get(token);
|
| | | MemberEntity member = JSON.parseObject(object, MemberEntity.class);
|
| | | String mId = member.getId().toString();
|
| | | |
| | | String uri = request.getRequestURI();
|
| | | log.info("#token : {}, uri : {}, mId : {}#", token, uri, mId);
|
| | | key = mId + "_" + uri;
|
| | | boolean flag = redisUtil.setNotExist(key, "1", 5);
|
| | | log.info("#mid : {}, flag : {}#", mId, flag);
|
| | | if (flag) {
|
| | | return joinPoint.proceed();
|
| | | } else {
|
| | | throw new FebsException("请勿重复提交");
|
| | | }
|
| | | }
|
| | |
|
| | | @After("submitRepeatPointCut(submitRepeat)")
|
| | | public void after(SubmitRepeat submitRepeat) {
|
| | |
|
| | | }
|
| | |
|
| | | private static String resolveToken(String token) {
|
| | | try {
|
| | | RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
|
| | | String[] tokens = StrUtil.split(rsa.decryptStr(token, KeyType.PrivateKey), "_");
|
| | | return tokens[0];
|
| | | } catch (Exception e) {
|
| | | return null;
|
| | | }
|
| | | }
|
| | | }
|
| | | //package cc.mrbird.febs.common.aspect;
|
| | | //
|
| | | //import cc.mrbird.febs.common.annotation.SubmitRepeat;
|
| | | //import cc.mrbird.febs.common.contants.AppContants;
|
| | | //import cc.mrbird.febs.common.exception.FebsException;
|
| | | //import cc.mrbird.febs.common.utils.RedisUtils;
|
| | | ////import cc.mrbird.febs.modules.api.entity.MemberEntity;
|
| | | //import cn.hutool.core.util.StrUtil;
|
| | | //import cn.hutool.crypto.asymmetric.KeyType;
|
| | | //import cn.hutool.crypto.asymmetric.RSA;
|
| | | //import com.alibaba.fastjson.JSON;
|
| | | //import lombok.extern.slf4j.Slf4j;
|
| | | //import org.aspectj.lang.ProceedingJoinPoint;
|
| | | //import org.aspectj.lang.annotation.*;
|
| | | //import org.springframework.stereotype.Component;
|
| | | //import org.springframework.web.context.request.RequestContextHolder;
|
| | | //import org.springframework.web.context.request.ServletRequestAttributes;
|
| | | //
|
| | | //import javax.annotation.Resource;
|
| | | //import javax.servlet.http.HttpServletRequest;
|
| | | //
|
| | | //@Slf4j
|
| | | //@Aspect
|
| | | //@Component
|
| | | //public class SubmitRepeatAspect {
|
| | | //
|
| | | // @Resource
|
| | | // private RedisUtils redisUtil;
|
| | | //
|
| | | // private String key;
|
| | | //
|
| | | // @Pointcut("@annotation(submitRepeat)")
|
| | | // public void submitRepeatPointCut(SubmitRepeat submitRepeat) {
|
| | | //
|
| | | // }
|
| | | //
|
| | | // @Before("submitRepeatPointCut(submitRepeat)")
|
| | | // public void before(SubmitRepeat submitRepeat) {
|
| | | // }
|
| | | //
|
| | | // @Around("submitRepeatPointCut(submitRepeat)")
|
| | | // public Object around(ProceedingJoinPoint joinPoint, SubmitRepeat submitRepeat) throws Throwable {
|
| | | // ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
| | | // HttpServletRequest request = attributes.getRequest();
|
| | | //
|
| | | // String headerToken = request.getHeader("token");
|
| | | // String token = resolveToken(headerToken);
|
| | | // if(StrUtil.isEmpty(token)) {
|
| | | // throw new FebsException("系统繁忙");
|
| | | // }
|
| | | // String object = (String)redisUtil.get(token);
|
| | | // MemberEntity member = JSON.parseObject(object, MemberEntity.class);
|
| | | // String mId = member.getId().toString();
|
| | | //
|
| | | // String uri = request.getRequestURI();
|
| | | // log.info("#token : {}, uri : {}, mId : {}#", token, uri, mId);
|
| | | // key = mId + "_" + uri;
|
| | | // boolean flag = redisUtil.setNotExist(key, "1", 5);
|
| | | // log.info("#mid : {}, flag : {}#", mId, flag);
|
| | | // if (flag) {
|
| | | // return joinPoint.proceed();
|
| | | // } else {
|
| | | // throw new FebsException("请勿重复提交");
|
| | | // }
|
| | | // }
|
| | | //
|
| | | // @After("submitRepeatPointCut(submitRepeat)")
|
| | | // public void after(SubmitRepeat submitRepeat) {
|
| | | //
|
| | | // }
|
| | | //
|
| | | // private static String resolveToken(String token) {
|
| | | // try {
|
| | | // RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
|
| | | // String[] tokens = StrUtil.split(rsa.decryptStr(token, KeyType.PrivateKey), "_");
|
| | | // return tokens[0];
|
| | | // } catch (Exception e) {
|
| | | // return null;
|
| | | // }
|
| | | // }
|
| | | //}
|