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> 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","无权限"); } }