Fixiaobai
2023-08-26 9e39021f891484b6accb2abc77d34c87bd65681a
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
package com.yuanchu.limslaboratory.aop;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuanchu.limslaboratory.annotation.RequestInfo;
import com.yuanchu.limslaboratory.exception.AuthException;
import com.yuanchu.limslaboratory.utils.ServletUtils;
import com.yuanchu.limslaboratory.vo.Result;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @Author 张宾
 * @Date 2023/8/25
 */
@Component
@Slf4j
@Aspect
public class AuthRequestAspect {
 
    @Pointcut("@annotation(com.yuanchu.limslaboratory.annotation.RequestInfo)")
    public void pointCut() {
    }
 
    //private ThreadLocal<ConcurrentHashMap<String, Map>> threadLocal;
 
    @SneakyThrows
    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        HttpServletRequest request = ServletUtils.getRequest();
        log.info("拦截=====》"+request.getServletPath());
        Class<?> clazz = joinPoint.getTarget().getClass();
        String clazzName = joinPoint.getTarget().getClass().getName();
        // 获取访问的方法名
        String methodName = joinPoint.getSignature().getName();
        // 获取方法所有参数及其类型
        Class[] argClz = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
        // 获取访问的方法对象
        Method method = clazz.getDeclaredMethod(methodName, argClz);
        // 判断当前访问的方法是否存在指定注解
        if (method.isAnnotationPresent(RequestInfo.class)) {
            RequestInfo annotation = method.getAnnotation(RequestInfo.class);
            System.out.println(annotation.name());
            System.out.println(annotation.type());
        }
        //throw new AuthException("400","无权限");
    }
 
 
 
 
}