2. 整合切面,参数拦截+过滤

简介: 111


2. 整合切面,参数拦截+过滤

免费使用

2. 整合切面,参数拦截+过滤

Java

运行代码复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

package cn.zhicall.web.aspect;


import com.zhicall.framework.core.common.utils.log.LogProxy;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.slf4j.Logger;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;

import java.util.Arrays;


/**

* @author hebo@zhicall.cn

* @version 1.0

* @date 2020/6/5 0005 10:48

* @Desc    入参前置拦截,借助Spring-AOP

*/

@Component

@Aspect

public class RequestParamsAspect {


protected final Logger logger = LogProxy.getLogger("REQUEST_PARAM_LOG");

//为了记录执行时间 方便调试 如果不需要可以去掉

ThreadLocal<Long> startTime = new ThreadLocal<>();


/*

    * 这样可以扫描controller路径下面全部

    * 我这里路径是com.test.controller

    *     .user(包)

    *                                 UserController...

    *                               .admin(包)

    *                                 AdminController...

   */

   @Pointcut("execution(public * cn.web.controller.*.*.*(..))")

public void pointCut() {}


/**

    * 参数进行限制或者拦截(后续可数据库存储或ELK)

    * @param joinPoint

    * @throws Throwable

    */

   @Before("pointCut()")

public void before(JoinPoint joinPoint) throws Throwable {

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = attributes.getRequest();

logger.info("请求来源:" + request.getRemoteAddr());

logger.info("请求URL:" + request.getRequestURL().toString());

logger.info("请求方式:" + request.getMethod());

logger.info("响应方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

logger.info("请求参数:" + Arrays.toString(joinPoint.getArgs()));


startTime.set(System.currentTimeMillis());

}


/**

    * 环绕执行(暂未使用)

    * 定义需要匹配的切点表达式,同时需要匹配参数

    * @param pjp

    * @return

    * @throws Throwable

    */

   @Around("pointCut()")

public Object around(ProceedingJoinPoint pjp) throws Throwable {

//这句必须有 往下执行方法

Object result = pjp.proceed();

logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));

return result;

}


/**

    * 后置通知

    * 在方法执行后执行 可以打印返回的数据 判断数据是否是自己需要的或脱敏

    * @param point

    */

   @After("pointCut()")

public void after(JoinPoint point) {

if (startTime.get() == null) {

startTime.set(System.currentTimeMillis());

}

logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));

// 清理线程池

startTime.remove();

}

}



2 人点赞

2

油炸小波

2020-07-10 16:40

2147

0

举报

分享到:

注册 / 登录 语雀进行评论

1/614字

关于语雀使用帮助数据安全服务协议English快速注册

油炸小波

微服务技术栈

搜索Ctrl + J

首页

目录

3-MongoDB常用命令

4-MongoDB索引知识

5-MongoDB实战演练

MyBatis

1.常见配置

2.映射关系(1-1 1-n n-n)

3.分页

汇总

SpringBoot

使用归纳总结

@Configuration

无标题

SpringBoot使用汇总

@Inherited

1. 整合Logback,滚动记录+多文件

2. 整合切面,参数拦截+过滤

3. 打包

4. 不定参数入参

5.跨域处理

6.鉴权

源码

SpringSecurity

SpringCloud(2024)

SpringCloud(2025)

SpringCloud(2026)

Zookeeper

Adblocker


相关文章
|
8月前
|
数据可视化 Java 关系型数据库
|
8月前
|
SQL 监控 机器人
|
开发工具 git 开发者
如何让现有的 Git 分支跟踪远程分支?
【8月更文挑战第15天】
2072 1
如何让现有的 Git 分支跟踪远程分支?
|
人工智能 自动驾驶 安全
Cosmos:英伟达生成式世界基础模型平台,加速自动驾驶与机器人开发
Cosmos 是英伟达推出的生成式世界基础模型平台,旨在加速物理人工智能系统的发展,特别是在自动驾驶和机器人领域。
1442 15
Cosmos:英伟达生成式世界基础模型平台,加速自动驾驶与机器人开发
|
消息中间件 存储 监控
云消息队列RabbitMQ版测评报告
在数字化快速发展的背景下,分布式系统的稳定性和可靠性至关重要。阿里云推出的云消息队列RabbitMQ解决方案,通过架构优化和一键部署,有效提升了系统的吞吐量和响应速度,降低了部署和维护成本。本文详细介绍了该方案的部署流程、方案验证,并提出了改进建议。
|
存储 C语言 Python
求1+2!+3!+…+20!的和
求1+2!+3!+…+20!的和
589 3
|
监控 数据可视化 安全
Axure智慧社区数据可视化大屏模板
在数字化时代,智慧社区的建设成为提升居民生活质量和优化管理效率的重要手段。我们推出Axure智慧社区数据可视化大屏模板(含5套),帮助社区管理者快速搭建高效、美观的大屏,实现数据的实时监控和智能分析。模板设计多样、高度可定制,支持实时数据更新,适用于社区管理中心、物业管理处、社区活动中心和安防监控室等多种场景。立即下载,开启智慧社区新篇章!
948 0
|
监控
USB 2.0 规范摘录1
USB 2.0 规范摘录
739 1
|
vr&ar 开发工具 图形学
Pico Neo 3教程☀️ 五、开发者工具:实时预览工具(Preview Tool)
Pico Neo 3教程☀️ 五、开发者工具:实时预览工具(Preview Tool)
|
安全 Java
Logback 实现日志链路追踪
Logback 实现日志链路追踪
549 1

热门文章

最新文章