import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * @author kelin.ll * @date on 2019/6/5 */ @Aspect @Component @Slf4j public class AuthAspect {/** * 这个切点的表达式需要根据自己的项目来写 * 说明: * execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?) * 修饰符匹配(modifier-pattern?) * 返回值匹配(ret-type-pattern)可以为*表示任何返回值,全路径的类名等 * 类路径匹配(declaring-type-pattern?) * 方法名匹配(name-pattern)可以指定方法名 或者 *代表所有, set* 代表以set开头的所有方法 * 参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“*”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的方法;(*,String) 表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数 * 异常类型匹配(throws-pattern?) * 其中后面跟着“?”的是可选项 * * 如: * 1)execution(* *(..)) 表示匹配所有方法 * 2)execution(public * com. savage.service.UserService.*(..)) 表示匹配com.savage.server.UserService中所有的公有方法 * 3)execution(* com.savage.server..*.*(..)) 表示匹配com.savage.server包及其子包下的所有方法 */ @Pointcut("execution(public * com.anole.manager.controller.RealTimeApiController.*(..))") public void auth() { } @Before("auth()") public void doBefore(JoinPoint joinPoint) { log.info("aop doBefore.."); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //url log.info("url={}", request.getRequestURI()); //method log.info("method={}", request.getMethod()); //ip log.info("ip={}", request.getRemoteAddr()); //类方法 log.info("classMethod={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); //参数 Enumeration<String> paramter = request.getParameterNames(); while (paramter.hasMoreElements()) { String str = (String)paramter.nextElement(); log.info(str + "={}", request.getParameter(str)); } } @Around("auth()") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { StopWatch stopWatch = new StopWatch(); // 耗时计算-开始 stopWatch.start(); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //参数 Enumeration<String> paramter = request.getParameterNames(); while (paramter.hasMoreElements()) { String str = (String)paramter.nextElement(); log.info(str + "={}", request.getParameter(str)); } // TODO 可在此处实现鉴权逻辑,修改返回response Object returnObj = proceedingJoinPoint.proceed(); // 耗时计算-结束 stopWatch.stop(); log.info("【{}方法】耗时:{}", proceedingJoinPoint.getSignature().getName(), stopWatch.getTotalTimeMillis()); return returnObj; } @After("auth()") public void doAfter() { log.info("aop doAfter"); } }