cm-cloud/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java

85 lines
2.9 KiB
Java
Raw Normal View History

package com.cm.common.aspect;
import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.base.SystemException;
import com.cm.common.utils.annotation.AnnotationUtil;
import com.google.inject.internal.cglib.core.$ClassInfo;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: ApiParamsAspect
* @Description: api接口
* @Author: WangGeng
* @Date: 2019/11/14 15:36
* @Version: 1.0
**/
@Aspect
@Component
@Order(1)
public class ApiParamsAspect {
private static final Logger LOG = LoggerFactory.getLogger(ApiParamsAspect.class);
@Pointcut("execution(public * com.cm..*Controller.*(..))")
public void controllerCutPoint() {
}
@Before("controllerCutPoint()")
public void beforeApiCutPoint(JoinPoint joinPoint) throws ParamsException {
Signature signature = joinPoint.getSignature();
Object[] args = joinPoint.getArgs();
Class<?> targetClazz = joinPoint.getTarget().getClass();
Method method = getMethod(signature.getName(), targetClazz.getMethods(), args);
if (method == null) {
return;
}
if (method.isAnnotationPresent(CheckRequestBodyAnnotation.class)) {
LOG.debug("校验参数");
for (Object arg : args) {
AnnotationUtil.checkField(arg);
}
}
}
private Method getMethod(String methodName, Method[] methods, Object[] args) {
for (Method method : methods) {
Class<?>[] parameterTypes = method.getParameterTypes();
// 方法名称相同
if (StringUtils.equals(methodName, method.getName())) {
// 参数长度相同
if (args.length == parameterTypes.length) {
// 参数类型顺序相同
boolean isThisMethod = true;
for (int argIndex = 0; argIndex < args.length; argIndex++) {
if (!parameterTypes[argIndex].isInstance(args[argIndex])) {
isThisMethod = false;
break;
}
}
if (isThisMethod) {
return method;
}
}
}
}
return null;
}
}