@Component
@Aspect
public class AopLog {
private static final Logger log = LoggerFactory.getLogger(AopLog.class);
@Pointcut("execution(* com.example.dream.user.controller.*.*(..))")
public void login(){
System.out.println("aspect");
}
@Before("login()")
public void before(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
StringBuffer sb = new StringBuffer();
sb.append("请求方法:" + request.getMethod());
sb.append(" 请求地址:" + request.getRequestURL());
sb.append(" 请求类、方法:" + joinPoint.getSignature());
sb.append(" 请求方法参数:");
Object[] args = joinPoint.getArgs();
for (Object obj : args) {
sb.append(obj);
}
log.info(sb.toString());
}
@AfterReturning(value = "login()", returning = "ret")
public void doAfterReturning(Object ret) throws Throwable {
log.info("请求返回 : {}", ret.toString());
}
@AfterThrowing(value = "login()",throwing ="throwable")
public void doAfterThrowing(JoinPoint joinPoint,Throwable throwable) {
Class clazz = joinPoint.getTarget().getClass();
String targetName = clazz.getSimpleName();
String methodName = joinPoint.getSignature().getName();
String logString = "异常退出方法: " + targetName + "." + methodName + ",返回结果是: " + throwable;
log.error(logString);
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AuthAnnotation {
AuthUser[] value();
}
public enum AuthUser {
AUTH_USER
}
@Order(1)
@Aspect
public class AuthAop {
@Before("@annotation(com.example.dream.annotation.AuthAnnotation)")
public void AuthBefor(JoinPoint joinPoint){
}
}