package com.fdkankan.openApi.component; import cn.hutool.core.util.StrUtil; import com.fdkankan.common.constant.ErrorCode; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.openApi.util.TokenUtil; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Objects; @Component @Aspect public class ValidateApiAOP { @Pointcut("@annotation(com.fdkankan.openApi.component.ValidateApi)") public void pointCut(){} @Around("pointCut()") public Object handle(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ //获取请求对象 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); //处理 //请求头 String authorization = request.getHeader("Authorization"); if(StrUtil.isEmpty(authorization)){ throw new BusinessException(ErrorCode.AUTH_FAIL); } if (!TokenUtil.validateToken(authorization)){ throw new BusinessException(ErrorCode.AUTH_FAIL); } //注解处理 Class clazz = proceedingJoinPoint.getTarget().getClass(); //方法签名 MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); Method method = clazz.getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes()); return proceedingJoinPoint.proceed(); } }