转:Spring Boot中使用AOP统一处理Web请求日志
在spring boot中,简单几步,使用spring AOP实现一个拦截器:
1、引入依赖:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- </dependency>
2、创建拦截器类(在该类中,定义了拦截规则:拦截com.xjj.web.controller包下面的所有类中,有@RequestMapping注解的方法。):
- /**
- * 拦截器:记录用户操作日志,检查用户是否登录……
- * @author XuJijun
- */
- @Aspect
- @Component
- public class ControllerInterceptor {
- private static final Logger logger = LoggerFactory.getLogger(ControllerInterceptor.class);
- @Value(“${spring.profiles}”)
- private String env;
- /**
- * 定义拦截规则:拦截com.xjj.web.controller包下面的所有类中,有@RequestMapping注解的方法。
- */
- @Pointcut(“execution(* com.xjj.web.controller..*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)”)
- public void controllerMethodPointcut(){}
- /**
- * 拦截器具体实现
- * @param pjp
- * @return JsonResult(被拦截方法的执行结果,或需要登录的错误提示。)
- */
- @Around(“controllerMethodPointcut()”) //指定拦截器规则;也可以直接把“execution(* com.xjj………)”写进这里
- public Object Interceptor(ProceedingJoinPoint pjp){
- long beginTime = System.currentTimeMillis();
- MethodSignature signature = (MethodSignature) pjp.getSignature();
- Method method = signature.getMethod(); //获取被拦截的方法
- String methodName = method.getName(); //获取被拦截的方法名
- Set<Object> allParams = new LinkedHashSet<>(); //保存所有请求参数,用于输出到日志中
- logger.info(”请求开始,方法:{}”, methodName);
- Object result = null;
- Object[] args = pjp.getArgs();
- for(Object arg : args){
- //logger.debug(“arg: {}”, arg);
- if (arg instanceof Map<?, ?>) {
- //提取方法中的MAP参数,用于记录进日志中
- @SuppressWarnings(“unchecked”)
- Map<String, Object> map = (Map<String, Object>) arg;
- allParams.add(map);
- }else if(arg instanceof HttpServletRequest){
- HttpServletRequest request = (HttpServletRequest) arg;
- if(isLoginRequired(method)){
- if(!isLogin(request)){
- result = new JsonResult(ResultCode.NOT_LOGIN, “该操作需要登录!去登录吗?\n\n(不知道登录账号?请联系老许。)”, null);
- }
- }
- //获取query string 或 posted form data参数
- Map<String, String[]> paramMap = request.getParameterMap();
- if(paramMap!=null && paramMap.size()>0){
- allParams.add(paramMap);
- }
- }else if(arg instanceof HttpServletResponse){
- //do nothing…
- }else{
- //allParams.add(arg);
- }
- }
- try {
- if(result == null){
- // 一切正常的情况下,继续执行被拦截的方法
- result = pjp.proceed();
- }
- } catch (Throwable e) {
- logger.info(”exception: ”, e);
- result = new JsonResult(ResultCode.EXCEPTION, “发生异常:”+e.getMessage());
- }
- if(result instanceof JsonResult){
- long costMs = System.currentTimeMillis() - beginTime;
- logger.info(”{}请求结束,耗时:{}ms”, methodName, costMs);
- }
- return result;
- }
- /**
- * 判断一个方法是否需要登录
- * @param method
- * @return
- */
- private boolean isLoginRequired(Method method){
- if(!env.equals(“prod”)){ //只有生产环境才需要登录
- return false;
- }
- boolean result = true;
- if(method.isAnnotationPresent(Permission.class)){
- result = method.getAnnotation(Permission.class).loginReqired();
- }
- return result;
- }
- //判断是否已经登录
- private boolean isLogin(HttpServletRequest request) {
- return true;
- /*String token = XWebUtils.getCookieByName(request, WebConstants.CookieName.AdminToken);
- if(“1”.equals(redisOperator.get(RedisConstants.Prefix.ADMIN_TOKEN+token))){
- return true;
- }else {
- return false;
- }*/
- }
- }
转:Spring Boot中使用AOP统一处理Web请求日志的更多相关文章
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- (转)Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)
一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- spring Boot使用AOP统一处理Web请求日志记录
1.使用spring boot实现一个拦截器 1.引入依赖: <dependency> <groupId>org.springframework.boot</grou ...
- AOP统一处理Web请求日志
<!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- springboot Aop 统一处理Web请求日志
1.增加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
随机推荐
- P4568 [JLOI2011]飞行路线 分层图
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为00到n-1n−1,一共有mm种航线,每种航线连接两个城市,并且 ...
- jquery手指触摸滑动放大图片的方法(比较靠谱的方法)
jquery手指触摸滑动放大图片的方法(比较靠谱的方法) <pre><!DOCTYPE html><html lang="zh-cn">< ...
- 面试开挂:近百道Java面试题整理
1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. Java被 ...
- nyoj 259-茵茵的第一课 (python, input, print)
259-茵茵的第一课 内存限制:64MB 时间限制:3000ms 特判: No 通过数:23 提交数:36 难度:0 题目描述: 茵茵今年已经六年级了,爸爸给她报了一个学习程序设计的班. 第一节课上, ...
- 搭建Nginx正向代理服务
需求背景: 前段时间公司因为业务需求需要部署一个正向代理,需要内网服务通过正向代理访问到外网移动端厂商域名通道等效果,之前一直用nginx做四层或者七层的反向代理,正向代理还是第一次配置,配置的过程也 ...
- Java的String类详解
Java的String类 String类是除了Java的基本类型之外用的最多的类, 甚至用的比基本类型还多. 同样jdk中对Java类也有很多的优化 类的定义 public final class S ...
- raid10配置流程(5块磁盘)
1.添加5块磁盘 (1)先添加一块磁盘 (2)在此基础上,再添加4块磁盘 2.使用mdadm命令创建RAID10,名称为“/dev/md0” -C 代表创建操作 -v 显示创建过程 ...
- web前端面试题总结(html、css)
1.对 WEB 标准以及 W3C 的理解与认识? 参考: 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外 链 css 和 js 脚本. 结构行为表现的分离.文件下载与页面速度更快.内容能 ...
- 生产者-消费者模型在Hudi中的应用
介绍 生产者-消费者模型用于解耦生产者与消费者,平衡两者之间的能力不平衡,该模型广泛应用于各个系统中,Hudi也使用了该模型控制对记录的处理,即记录会被生产者生产至队列中,然后由消费者从队列中消费,更 ...
- Alibaba Nacos 学习(五):K8S Nacos搭建,使用nfs
Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...