SpringBoot Aop打印参数

复制代码
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");
    }
}
复制代码

 

posted @   GisClub  阅读(1520)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示