AOP终止原方法的执行,并修改参数
定义注解
package com.cyboil.Annotation;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CheckAuthByCardNo {
}
定义切面
package com.cyboil.Annotation;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cyboil.common.baseframe.core.plm.beans.ResultBean;
import com.cyboil.mapper.CustSelf_ICCustMapper;
import com.cyboil.model.CustSelf_ICCRUserCard;
import com.cyboil.service.CustSelf_CustService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
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.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
@Aspect
@Component
@Slf4j
public class CheckAuthByCardNoAspect {
@Autowired
private CustSelf_ICCustMapper _ICCustMapper;
@Autowired
private CustSelf_CustService custService;
// @Before("@annotation(com.cyboil.Annotation.CheckAuthByCardNo)")
@SneakyThrows
@Around("@annotation(com.cyboil.Annotation.CheckAuthByCardNo)")
public Object Around(ProceedingJoinPoint joinPoint) {
/*AOP终止原方法的执行*/
// ResultBean resultBean = new ResultBean();
// //获取接口的参数
// Object[] args = joinPoint.getArgs();
// String argsJsonStr = JSONObject.toJSONString(args);
// JSONArray argsJson = JSONObject.parseObject(argsJsonStr, JSONArray.class);
// log.info(argsJson.toJSONString());
// String cardSerialNo = null;
// for (int i = 0; i < argsJson.size(); i++) {
// JSONObject jsonObject = argsJson.getJSONObject(i);
// if (jsonObject.containsKey("Data") && jsonObject.getJSONObject("Data").containsKey("Param")) {
// cardSerialNo = jsonObject.getJSONObject("Data").getJSONObject("Param").getString("CardSerialNo");
// }
// }
// CustSelf_ICCRUserCard userCard = _ICCustMapper.getCustNo(cardSerialNo, null);
// if (userCard != null) {
// /*校验成功,继续执行方法*/
// return joinPoint.proceed();
// } else {
// /*校验失败,可以返回你失败的信息也可以直接抛出校验失败的异常*/
// resultBean.setResult(1);
// resultBean.setMsg("未授权");
// return resultBean;
// }
/*修改接口参数*/
try {
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
JSONObject argJson = (JSONObject) args[0];
String token = argJson.getString("Token");
argJson.put("Token", "------");
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("Data", new JSONObject());
Object[] objects = new Object[1];
objects[0] = jsonObject;
/*objects是Controller接收参数*/
Object ret = joinPoint.proceed(objects);
/*ret是CON*/
return ret;
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
@SneakyThrows
// @Before("@annotation(com.cyboil.Annotation.CheckAuthByCardNo)")
public void Before(JoinPoint point, CheckAuthByCardNo cleanerTimeCheck) {
}
}