aop拦截所有请求并打印请求方法参数

news2024/9/22 5:42:50

效果图

 代码

package com.hxnwm.ny.diancan.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;

/**
 * @ClassName OperationLogAspect
 * @Description TODO
 * @Author wdj
 * @Date 2023/5/31 11:23
 * @Version
 */
@Aspect
@Component
@Slf4j   // lombok中日志注解
public class LogAspect {
    @Resource
    private LogAspectMethod logAspectMethod;
    /**
     * 定义切入点表达式
     * 访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
     * 权限修饰符可以使用默认 第一个*表示返回值类型  ..表示当前包以及其子包下 第二个*表示任意方法 (..)表示任意参数列表
     */
    private final String POINTCUT = "execution(* com.hxnwm.ny.diancan.controller..*(..))";

    /**
     * 前置通知,方法之前执行
     * @param joinPoint
     */
    @Before(POINTCUT)
    public void doBefore(JoinPoint joinPoint) {
        // 获取当前的HttpServletRequest对象
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        logAspectMethod.doBeforeMethod(attributes,joinPoint);
    }

    /**
     * 返回通知 正常结束时进入此方法
     *
     * @param ret
     */
    @AfterReturning(returning = "ret", pointcut = POINTCUT)
    public void doAfterReturning(Object ret) {
        logAspectMethod.doAfterReturningMethod(ret);
    }

    /**
     * 异常通知: 1. 在目标方法非正常结束,发生异常或者抛出异常时执行
     *
     * @param throwable
     */
    @AfterThrowing(pointcut = POINTCUT, throwing = "throwable")
    public void doAfterThrowing(Throwable throwable) {
        logAspectMethod.doAfterThrowingMethod(throwable);
    }
}
package com.hxnwm.ny.diancan.common.aspect;

import com.hxnwm.ny.diancan.common.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

/**
 * @ClassName LogAspectMethod
 * @Description TODO
 * @Author wdj
 * @Date 2023/6/1 13:51
 * @Version
 */
@Slf4j   // lombok中日志注解
@Component
public class LogAspectMethod {
    /**
     * 进入方法时间戳
     */
    private Long startTime;
    /**
     * 方法结束时间戳(计时)
     */
    private Long endTime;

    @Resource
    private JwtUtils jwtUtils;
    @Async("asyncExecutor")
    public void doBeforeMethod(ServletRequestAttributes attributes, JoinPoint joinPoint){
        if (Objects.isNull(attributes) || Objects.isNull(attributes.getRequest()) || Objects.isNull(joinPoint)) {
            log.info("请求信息不能为空");
            return;
        }
        HttpServletRequest request = attributes.getRequest();

        // 打印请求的内容
        startTime = System.currentTimeMillis();
        Integer userId=0;//0无user
        // 从请求头中获取token
        String token = request.getHeader("token");
        if(Objects.nonNull(token) && Objects.nonNull(jwtUtils)){
            // 解密token
            userId = this.jwtUtils.getUserId(token);
        }
        StringBuffer url=new StringBuffer();
        if(Objects.nonNull(request.getRequestURL())){
            url=request.getRequestURL();
        }
        String method= "",addr="",contentType="";
        if(Objects.nonNull(request.getMethod())){
            method=request.getMethod();
        }
        if(Objects.nonNull(request.getRemoteAddr())){
            addr=request.getRemoteAddr();
        }
        if(Objects.nonNull(request.getContentType())){
            contentType=request.getContentType();
        }

        log.debug("请求开始时间:{}===Url : {}===方式 : {}===ip : {}===内容类型 : {}===用户id:{}===类:[{}]===方法:[{}]===参数 : {}", LocalDateTime.now(),url.toString(),method,addr,contentType,userId,joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
        // 系统信息
    }

    @Async("asyncExecutor")
    public void doAfterReturningMethod(Object ret){
        endTime = System.currentTimeMillis();
        log.debug("请求结束时间 : {}===耗时 : {}毫秒===请求返回 : {}",LocalDateTime.now(),(endTime - startTime), ret);
    }


    @Async("asyncExecutor")
    public void doAfterThrowingMethod(Throwable throwable){
        log.error("发生异常时间 : {},抛出异常 : {}",LocalDateTime.now(),throwable.getMessage());
    }

}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/729137.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

redis的主从复制、哨兵模式和集群模式搭建

概述 redis主从复制搭建 主从复制的作用 主从复制流程 搭建Redis 主从复制 修改 Redis 配置文件(Master节点操作) 修改 Redis 配置文件(Slave节点操作) 验证主从效果 ​编辑 Redis 哨兵模式 哨兵模式的作用 故障转移机制 …

idea切换Git分支时保存未提交的文件

问题描述 现在需要开发一个新功能A时,我们需要从Dev分支上创建一个新的功能分支tenant,然后我们就在这个分支上进行开发。假设有一天,你正在开发,本地已经在tenant上修改了几个文件,但是功能还没有完全开发完成&#…

游戏反调试方案解析与frida/IDA框架分析

近来年,游戏黑灰产攻击趋势呈现出角度多样化的特点。据FairGuard游戏安全数据分析发现,游戏黑灰产攻击以工作室、定制注入挂、内存修改器、模拟点击、破解等形式为主。 游戏安全风险分布占比图 对于一款游戏而言,上述的风险中,被…

Kubernetes(k8s)实战:深入详解Volume,详解k8s文件同步存储

文章目录 一、Volume1、什么是Volume2、Host类型volume实战(不推荐)(1)小总结 二、PersistentVolume持久化volume(推荐)1、什么是PersistentVolume2、什么是PersistentVolumeClaim3、Pod中如何使用PVC 三、…

初中学物理实验室教学装备配置标准

初中物理实验室建设,以现行义务教育物理教科书为基本参照,以学生学科核心素养发展为基本遵循,以加强实验等实践性教学活动,落实立德树人根本任务为目标。因此,初中物理实验室的建设实施过程中,需结合校情、…

PCB封装设计指导(三)如何创建PAD

PCB封装设计指导(三)如何创建PAD 当我们完全看完了Datasheet之后,确定了需要建立的封装类型,以及尺寸之后,这一步就可以开始创建封装的PAD了。 下面介绍如何创建各种类型的PAD和一些技巧和注意点,包括创建flash的注意事项。 1.如何创建PAD 1. 打开pad designer ,根据尺…

STL源码刨析 string实现

目录 一. string 类介绍 二. string 的简单实现 1. 类内成员变量 2. Member functions string ~string operator string(const string& str) 3. Capacity size capacity empty clear reserve resize 4.Modifiers push_back append operator insert era…

【回溯算法part05】| 491.递增子序列、46.全排列、47.全排列||

目录 🎈LeetCode491.递增子序列 🎈LeetCode46.全排列 🎈LeetCode47.全排列|| 🎈LeetCode491.递增子序列 链接:491.递增子序列 给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列&#xf…

【玩转Linux操作】详细讲解Linux的 权限 操作

🎊专栏【​​​​​​​玩转Linux操作】 🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。 🎆音乐分享【Love Story】 🥰欢迎并且感谢大家指出小吉的问题🥰 文章目录 🍔权限的基本介绍⭐具体分析&…

【复习1-2天的内容】【我们一起60天准备考研算法面试(大全)-第六天 6/60】

专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客,如有问题交流,欢迎评论区留言,一定尽快回复!(大家可以去看我的专栏,是所有文章的目录)   文章字体风格: 红色文字表示&#…

网络编程---day4

广播发送方: 广播接收方: 组播发射方: 组播接收方:

OpenCV的remap实现图像垂直翻转

以下是完整的代码: #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream>int main() {

从“存算一体”到“存算分离”:金融核心数据库改造的必经之路

科技云报道原创。 近年来&#xff0c;数据库国产化趋势愈发明显&#xff0c;上百家金融业试点单位在数据库国产化的进程中&#xff0c;进一步增强信心&#xff0c;向50%国产化率大步迈进。 但随着数据库国产化的深入&#xff0c;一些金融机构采用国产数据库服务器本地盘的“存…

【微信小程序创作之路】- 小程序中WXML、JS、JSON、WXSS作用

【微信小程序创作之路】- 小程序中WXML、JS、JSON、WXSS作用 第三章 微信小程序WXML、JS、JSON、WXSS作用 文章目录 【微信小程序创作之路】- 小程序中WXML、JS、JSON、WXSS作用前言一、WXML是什么&#xff1f;二、JS是什么&#xff1f;三、JSON是什么&#xff1f;四、WXSS是什…

IPC 进程间通讯 (1)

目录 1.1 为什么要通信 1.2 为什么能通信 2.1 进程间通信机制的结构 2.2 进程间通信机制的类型 2.3 进程间通信机制的接口设计 3.1 SysV共享内存 3.2 POSIX共享内存 3.3 共享内存映射 3.4 Android ION 3.5 dma-buf heaps 3.6 匿名管道 3.7 命名管道 3.8 SysV消息队列…

基于Java实验室考勤管理系统设计实现(源码+lw+部署文档+讲解等)

博主介绍&#xff1a;✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专…

buuctf pwn入门1

目录 1. test_your_nc(简单nc ) pwn做题过程 2. rip(简单栈溢出) 3. warmup_csaw_2016(栈溢出 覆盖Return_Address) 4. ciscn_2019_n_1(栈溢出 浮点数十六进制) (1) 覆盖v2值 (2) 利用system("cat /flag"); 5. pwn1_sctf_2016(字符逃逸栈溢出 32位) 6. jarvis…

实现【Linux--NTP 时间同步服务搭建】

实现【Linux--NTP 时间同步服务搭建】 &#x1f53b; 前言&#x1f53b; 一、NTP 校时&#x1f530; 1.1 NTP 服务校时与 ntpdate 校时的区别&#x1f530; 1.2 NTP 校时服务搭建&#x1f530; 1.2.1 确认 ntp 的安装&#x1f530; 1.2.2 配置 ntp 服务&#x1f530; 1.2.3 启动…

QTday1

#include "widget.h"Widget::Widget(QWidget *parent): QWidget(parent) {this->resize(500,600);this->setFixedSize(500,600);//设置窗口标题this->setWindowTitle("盗版qq");//设置窗口图标this->setWindowIcon(QIcon("D://QQ下载//ic…

【C++STL】list的反向迭代器

list的反向迭代器 文章目录 list的反向迭代器reverse.h疑问1&#xff1a;为什么在迭代器当中不需要写深拷贝、析构函数疑问2&#xff1a;为什么在迭代器当中需要三个模板参数&#xff1f;疑问3&#xff1a;反向迭代器是怎么实现的&#xff1f;疑问4&#xff1a;为什么*解引用不…