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.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class PermissionAspect {
@Autowired
private ObjectMapper objectMapper;
/*
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return objectMapper;*/
//@Pointcut("execution(* cn.mwee.wpos.service..*(..))")
@Pointcut("execution(@cn.test.annotation.Permission * *(..))")
public void permissionPointcut() {}
@Around("permissionPointcut()")
public CommonResponse checkPermission(ProceedingJoinPoint joinPoint){ //ProceedingJoinPoint joinPoint
CommonResponse response=new CommonResponse();
try {
// 执行的类
String exeType = null;
// 执行的方法
String exeMethod = null;
response.getErr().setErrNo(ErrorCode.NO_PERMISSION).setErrMsg(ErrorDesc.NO_PERMISSION);
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
exeType = signature.getDeclaringType().getCanonicalName();
exeMethod = signature.getMethod().getName();
Class<?> clazz = signature.getMethod().getReturnType();
TokenAuth tokenAuth = AnnotationUtils.getAnnotation(signature.getMethod(), TokenAuth.class);
if (authAnnotation == null) {
tokenAuth =AnnotationUtils.getAnnotation(signature.getDeclaringType(), TokenAuth.class);
}
JavaType javaType = agentMapper.getTypeFactory().constructType(signature.getMethod().getGenericReturnType());
System.out.println(exeType + " " + exeMethod);
return objectMapper.readValue(newRespone, javaType);
//return response;
//return (CommonResponse)joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return response;
}
}
Spring AspectJ Aop Annotation
于 2017-04-06 16:11:06 首次发布