zainali5120
2020-10-28 8fda5eddb56178a0c1e0e247b5c7f45ffc739c92
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.xcong.excoin.common.aop;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.annotations.SubmitRepeat;
import com.xcong.excoin.common.contants.AppContants;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.configurations.properties.SecurityProperties;
import com.xcong.excoin.utils.MessageSourceUtils;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
/**
 * @author wzy
 * @date 2020-04-16 22:01
 **/
@Slf4j
@Aspect
@Component
public class SubmitRepeatAspect {
 
    @Resource
    private RedisUtils redisUtil;
    @Resource
    private SecurityProperties securityProperties;
 
    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 token = request.getHeader("token");
        String bearerToken = request.getHeader(AppContants.TOKEN_HEADER);
        String rsaToken = bearerToken.replace(AppContants.TOKEN_START_WITH, "");
        RSA rsa = new RSA(securityProperties.getPrivateKey(), null);
        String[] tokens = StrUtil.split(rsa.decryptStr(rsaToken, KeyType.PrivateKey), "_");
        String token = tokens[0];
        String uri = request.getRequestURI();
        Long mId = LoginUserUtils.getAppLoginUser().getId();
        //String mId = (String) redisUtil.get(token);
        //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) {
            Object result = joinPoint.proceed();
            redisUtil.del(key);
            return result;
        } else {
            return Result.fail(MessageSourceUtils.getString("submit_repeat"));
        }
    }
 
    @After("submitRepeatPointCut(submitRepeat)")
    public void after(SubmitRepeat submitRepeat) {
 
    }
 
    @AfterThrowing(throwing = "ex", pointcut = "submitRepeatPointCut(submitRepeat)")
    public void afterThrows(JoinPoint jp, Exception ex, SubmitRepeat submitRepeat) throws Exception {
        log.error("#submit repeat error:#", ex);
        redisUtil.del(key);
        throw new Exception("系统繁忙");
    }
}