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");
}
}
SpringBoot Aop打印参数的更多相关文章
- Aop 打印参数日志时,出现参数序列化异常。It is illegal to call this method if the current request is not in asynchron
错误信息: nested exception is java.lang.IllegalStateException: It is illegal to call this method if the ...
- SpringBoot AOP处理请求日志处理打印
SpringBoot AOP处理请求日志处理打印 @Slf4j @Aspect @Configuration public class RequestAopConfig { @Autowired pr ...
- springboot+aop切点记录请求和响应信息
本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...
- springboot aop 自定义注解方式实现完善日志记录(完整源码)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...
- springBoot AOP学习(一)
AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的 ...
- aop 打印请求信息
项目中使用 AOP 打印请求信息,打印响应信息.package com.example.aspect; import com.alibaba.fastjson.JSON;import com.goog ...
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
- springboot + aop + Lua分布式限流的最佳实践
整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 一.什么是限流?为什么要限流? 不知道大家有没有做过帝都的地铁, ...
随机推荐
- Odoo中的ORM API(模型数据增删改查)
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826214.html 一:增 1:create():返回新创建的记录对象 self.create({'na ...
- React源码 React.Component
React中最重要的就是组件,写的更多的组件都是继承至 React.Component .大部分同学可能都会认为 Component 这个base class 给我们提供了各种各样的功能.他帮助我们去 ...
- opencv想到的
opencv是用C++写的库,包了多种语言接口,包括C,C++,python,java等. OpenCV 是一个开放源代码的计算机视觉库,目前在科研和开发中被广泛使用.OpenCV 由一系列 C 函数 ...
- [原创]VBA实现汇总excel,将多个Excel文件内容复制到一个Excel文件中
功能:遍历用户指定的文件夹,把文件夹中所有的excel文件的第一个表格的数据复制到本excel文件中.注意,每个excel文件中有效数据行的判断标准是A列的最后一个有数据的单元格的行号,比如A列到第1 ...
- 如何开发出优秀的APICloud应用
APICloud定制平台项目实施规范 APICloud应用优化策略Top30 如何开发出运行体验良好.高性能的App 如何开发出客户满意.能够顺利交付的App 1. 引擎或模块问题: 遇到应用层无法解 ...
- Lexicographical Substring Search SPOJ - SUBLEX (后缀自动机)
Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...
- Hibernate的批量查询——Criteria查询所有、条件、分页、统计(聚合函数)、排序
1.查询所有的学生信息: public static void testSel() { Session session = HibernateUtils.openSession(); Transact ...
- class-transformer 方便的json 对象转class
在我们日常web开发中经常碰到需要json 到类的处理,因为json 就是普通的数据,没有处理能力,class 具有业务处理能力, 但是需要数据的支持,class-transformer 刚好做为了一 ...
- benchmarkdotnet dotnet 基准测试类库试用(一)
使用基准测试对于我们应用的性能优化是比较好的方式,可以快速看出优化的结果同时可以给出报告结果 benchmarkdotnet 是dotnet 版本的一个工具,以下是一个简单的试用 环境准备 我使用的是 ...
- sql server 性能优化的几个专家工具
以下为一个方便的sql server 分析的专家工具 sp_WhoIsActive 提供了方便的文档说明 参考网站 http://whoisactive.com/docs/ dba_BlockTrac ...