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. rest framework 之渲染器

    根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件. 用户请求头: Accept:text/html,application/xhtml+xml,application/xml;q=0 ...

  2. SpringCloud2.0 Eureka Server 服务中心 基础教程(二)

    1.创建[服务中心],即 Eureka Server 1.1.新建 Spring Boot 工程,工程名称: springcloud-eureka-server 1.2.工程 pom.xml 文件添加 ...

  3. 基于SCRUM方法实践的西油计科党建设计与实现-个人实践流程清单

    基于SCRUM方法实践的西油计科党建设计与实现 个人实践流程清单 一.Alpha版本冲刺个人在SCRUM团队任务清单: 时间 我这个三天做了什么 实际解决燃尽图项目数量 我遇到了什么问题 我下一个三天 ...

  4. css overflow失效的原因

    声明 转载自https://my.oschina.net/xuqianwen/blog/540587 项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分 ...

  5. selenium入门知识

    自动化测试 重复测试.性能测试.压力测试 快速.可靠.可重复.可程序化.广泛的 自动化测试适合场合 回归测试.更多更频繁的测试.手工测试无法实现的工作.跨平台产品的测试.重复性很强的操作 不适合场合 ...

  6. thinkPHP5框架路由常用知识点汇总

    一.路由的模式 普通模式(默认pathinfo,不解析路由) 'url_route_on' => false 混合模式(pathinfo+解析路由) 'url_route_on' => t ...

  7. Spring Cloud 之 Feign 知识点:封装了 REST 调用

    Feign Client 会在底层根据你的注解,跟你指定的服务建立连接.构造请求.发起请求.获取响应.解析响应,等等. Feign 的一个关键机制就是使用了动态代理. 首先,如果你对某个接口定义了 @ ...

  8. PL/SQL Developer 快捷键

    前面我有分享了一个PLSQL美化规则,其实通过统一的美化SQL,把这里SQL写在Java代码里可以比较容易阅读代码,且保持良好得编码风格. 在工作中我们也经常使用PLSQL来写一SQL,有些常用的SQ ...

  9. Intellij IDEA如何生成JavaDoc

    JavaDoc是一种将注释生成HTML文档的技术. 1.使用javadoc命令生成文档 首先了解一下javadoc指令的用法 用法: javadoc [options] [packagenames] ...

  10. Euclid`s Game

    题目 给定两个整数 a 和 b,Stan和Ollie轮流从较大的数字中减去较小的数的倍数.这里的倍数是指1倍.2倍这样的整数倍,并且相减后的结果不能小于0.Stan先手,在自己的回合将其中一个数变成零 ...