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打印参数的更多相关文章

  1. 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 ...

  2. SpringBoot AOP处理请求日志处理打印

    SpringBoot AOP处理请求日志处理打印 @Slf4j @Aspect @Configuration public class RequestAopConfig { @Autowired pr ...

  3. springboot+aop切点记录请求和响应信息

    本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...

  4. springboot aop 自定义注解方式实现完善日志记录(完整源码)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...

  5. springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)

    https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...

  6. springBoot AOP学习(一)

    AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的 ...

  7. aop 打印请求信息

    项目中使用 AOP 打印请求信息,打印响应信息.package com.example.aspect; import com.alibaba.fastjson.JSON;import com.goog ...

  8. 使用SpringBoot AOP 记录操作日志、异常日志

    平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...

  9. springboot + aop + Lua分布式限流的最佳实践

    整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 一.什么是限流?为什么要限流? 不知道大家有没有做过帝都的地铁, ...

随机推荐

  1. virt-install创建虚拟机并制作成模板

    一.使用virt-install创建新的虚拟机 virt--template --ram --vcpu= --virt-type kvm --cdrom=/Data/kvm/iso/CentOS-.i ...

  2. dfs 正则表达式

    192. 通配符匹配 中文 English 判断两个可能包含通配符“?”和“*”的字符串是否匹配.匹配规则如下: '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个串完 ...

  3. 云打印 对Echo的Beta产品测试报告

    云打印 对Echo的Beta产品测试报告 课程名称:软件工程1916|W(福州大学) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标:作业集合 团队队员 队员学号 队员姓名 个人博 ...

  4. 洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)

    题目描述 约翰和贝茜在玩一个方块游戏.编号为 1\ldots n 1-n 的 n n ( 1 \leq n \leq 30000 1≤n≤30000 )个方块正放在地上,每个构成一个立方柱. 游戏开始 ...

  5. 20180527模拟赛T1——新田忌赛马

    [问题描述] (注:此题为d2t2-难度) 田忌又在跟大王van赛马的游戏 田忌与大王一共有2n匹马,每个马都有一个能力值x,1<=x<=2n且每匹马的x互不相同.每次田忌与大王放出一匹马 ...

  6. Go语言 - 结构体 | 方法

    自定义类型和类型别名 自定义类型 在Go语言中有一些基本的数据类型,如string.整型.浮点型.布尔等数据类型, Go语言中可以使用type关键字来定义自定义类型. 自定义类型是定义了一个全新的类型 ...

  7. 内存模型学习-- Container Executor task之间的关系

    (分割线前的都是废话) java8内存模型: http://www.cnblogs.com/paddix/p/5309550.html http://www.cnblogs.com/dingyings ...

  8. codevs 2803 爱丽丝·玛格特罗依德

    二次联通门 : codevs 2803 爱丽丝·玛格特罗依德 /* codevs 2803 爱丽丝·玛格特罗伊德 高精 + 找规律 显然, 能拆3就多拆3 不能拆就拆2 注意特判一下 */ #incl ...

  9. CF241E Flights 题解

    题目 做了一下这道题,突然发现自己忘了差分约束,赶紧复习一下. 设当前有n个变量 a1,a2,...,an ,有若干组限制形如 ai≤aj+k (其中k为常数),则由点j向点i连一条边权为k的边,再从 ...

  10. LCA的几种做法

    P3379 LCA $ 1:$蜗牛爬式 void dfs(int u,int fa) { f[u]=fa;//预处理father for(int i=head[u]; i; i=e[i].nxt) i ...