从aop中获取被拦截方法中的参数

@Aspect
public class CacheManager {

}


@Before("execution(* com.aliexpress.social.game.cointree.cointree.gateway.application.*.*(..))")
public void migrateCheck(JoinPoint joinPoint) {
String userId = getUserIdFromAop(jointPoint);
}

//从拦截的参数中获取用户id
private String getUserIdFromAop(JoinPoint joinPoint){
//https://www.cnblogs.com/yln20170705/p/10511743.html

//类名
String clazzName = joinPoint.getTarget().getClass().getName();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//方法名
String methodName = methodSignature.getName();
//参数名数组
String[] parameters = methodSignature.getParameterNames();
//参数值
Object[] args = joinPoint.getArgs();

//获取参数名对应数组下标
RequestInfoDTO infoDTO = null;
String userId = null;

//infoDTO是被拦截的发方法的参数名,例如:plant(RequestInfoDTO infoDTO,Long cropId)
int paramIndex = ArrayUtils.indexOf(parameters,"infoDTO");
if (paramIndex != -1){
//参数info在方法列表中,位于第一个位置
infoDTO = (RequestInfoDTO)args[0];
userId = infoDTO.getUserId();

}
log.info("getUserIdFromAop clazzName = {} methodName = {} parameters = {} args = {} userId = {} ",
new Object[] {clazzName, methodName, JSON.toJSONString(parameters), JSON.toJSONString(args), userId});

return userId;

}

posted @ 2020-06-29 21:54  为爱奋斗不息  阅读(1073)  评论(0编辑  收藏  举报