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方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 一.什么是限流?为什么要限流? 不知道大家有没有做过帝都的地铁, ...
随机推荐
- HTTP认识
一.相关名词解释 1. 超文本:是指包含指向其他文档的超链接的文本 2. 万维网:简称web,是一个分布式的超媒体系统,它是超文本系统的扩充,以客户-服务器方式工作 3. 超媒体:文档包含文本,图片, ...
- 性能测试基础---事务&检查点&思考时间&集合点
性能测试脚本的增强:·参数化·关联·事务·检查点·思考时间·集合点 ·事务:事务的引入是为了度量相关的业务请求的响应时间和吞吐量指标.在LR中,事务是通过两个事务函数来实现的. lr_start_tr ...
- Caused by: java.lang.IllegalStateException: duplicate key: datasource
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/applicatio ...
- spring mvc @RequestMapping method 不写的话,默认GET、POST都支持,根据前端方式自动适应
@RequestMapping(value="/") method 不写的话,默认GET.POST都支持,根据前端方式自动适应.
- NameValueCollectionValueProvider
NameValueCollectionValueProvider provider = new NameValueCollectionValueProvider(nameValueCollection ...
- 推荐系统(recommender systems):预测电影评分--构造推荐系统的一种方法:低秩矩阵分解(low rank matrix factorization)
如上图中的predicted ratings矩阵可以分解成X与ΘT的乘积,这个叫做低秩矩阵分解. 我们先学习出product的特征参数向量,在实际应用中这些学习出来的参数向量可能比较难以理解,也很难可 ...
- 11、Python函数基础(定义函数、函数参数、匿名函数)
函数先定义函数,后调用 一.定义函数: 1.简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数. 函 ...
- map test
// mapTest.cpp : Defines the entry point for the console application. // #include "stdafx.h&quo ...
- 简单JSON
JSON是什么 JavaScript Object Notation(JavaScript 对象表示),是一种存储和交换文本信息的语法,它独立程序语言,是轻量级的文本数据交换格式,比XML更小.更快, ...
- type of的返回值有哪些
typeof 10; // number typeof 'time'; //string typeof undefined; // undefined typeof null; // object t ...