AOP实现操作日志记录
二、代码实现
1.自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLog {/*** 方法** @return*/String method();
}
2.ThreadLocal存储httpServletRequest
public class RequestManager {private static ThreadLocal<HttpServletRequest> threadLocal = new ThreadLocal<HttpServletRequest>();/*** 当前线程加入request** @param request*/public static void setHttpServletRequest(HttpServletRequest request) {if (request != null) {threadLocal.set(request);}}/*** 当前线程获取request*/public static HttpServletRequest getHttpServletRequest() {return threadLocal.get();}/*** 清理request,释放空间*/public static void removeHttpServletRequest() {threadLocal.remove();}
}
public class CommonFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {try {RequestManager.setHttpServletRequest(request);filterChain.doFilter(request, response);} finally {//防止出现内存泄露RequestManager.removeHttpServletRequest();}}
}
4.切面存储操作日志
@Service
@Aspect
public class OpLogSaveAspect {@Resourceprivate RedisSentinelService cartCacheService;@Resourceprivate IOpLogService opLogService;@Pointcut(value = "@annotation(com.dfire.soa.consumer.faw.annotation.OperationLog) ")public void operationLog() {}@Around(value = "operationLog()")public Object aroundService(ProceedingJoinPoint pjp) throws Throwable {Object obj;try {obj = pjp.proceed();saveOpLog(pjp);} catch (Throwable t) {throw t;}return obj;}private void saveOpLog(ProceedingJoinPoint pjp) {//获取用户信息HttpServletRequest request = RequestManager.getHttpServletRequest();if (request == null) {return;}AccountInfo currentUser = currentAccount(request);if (currentUser == null) {return;}MethodSignature methodSignature = (MethodSignature) pjp.getSignature();String method = this.getMethodAnnotation(methodSignature, pjp.getTarget().getClass());if (StringUtils.isBlank(method)) {return;}Object[] objects = pjp.getArgs();if (objects == null || objects.length == 0) {return;}List<Object> params = Lists.newArrayList();for(Object object : objects){if(object instanceof HttpServletRequest){continue;}params.add(object);}OpLogDTO opLogDTO = new OpLogDTO();opLogDTO.setOperator(currentAccount(request).getUsername());opLogDTO.setRequestUrl(request.getRequestURL().toString());opLogDTO.setMethod(method);opLogDTO.setParam(JSON.toJSONString(params));opLogDTO.setIp(request.getRemoteHost());opLogService.recordOpLog(opLogDTO);}/*** 取方法注释.** @param cls* @return the method annotation* @throws SecurityException* @throws NoSuchMethodException*/String getMethodAnnotation(MethodSignature methodSignature, Class<? extends Object> cls) {Method method = methodSignature.getMethod();try {method = cls.getMethod(method.getName(), method.getParameterTypes());OperationLog operationLog = method.getAnnotation(OperationLog.class);if (operationLog != null) {return operationLog.method();}} catch (NoSuchMethodException e) {} catch (SecurityException e) {}return null;}private AccountInfo currentAccount(HttpServletRequest request) {String token = request.getParameter("token");// 把 token 当作key 值从 redis 里面获取return JSON.parseObject(cartCacheService.get(Constants.TokenKey.VERIFY_CODE_TOKEN_KEY + token), AccountInfo.class);}}
@RequestMapping(value = "/v1/modify", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@OperationLog(method = "modifyActivity")
public ResultMap modify(HttpServletRequest request,@RequestBody PreSellActivityModifyDTO preSellActivityModifyDTO) throws Exception {
数据库存储如下:
AOP实现操作日志记录相关推荐
- JPOM - AOP+自定义注解实现操作日志记录
文章目录 地址 版本 源码解析-AOP+自定义注解实现操作日志记录 地址 Gitee: https://gitee.com/dromara/Jpom 官网: https://jpom.io/ 一款简而 ...
- 我们已经不用AOP做操作日志了!
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 前言 用户在操作我们系统的过程中,针对一些重要的业务数据进行增删改 ...
- 我们已经不用AOP做操作日志了! | 原力计划
来源 | JAVA葵花宝典 责编 | 王晓曼.Carol 头图 | CSDN下载自东方IC 前言 用户在操作我们系统的过程中,针对一些重要的业务数据进行增删改查的时候,我们希望记录一下用户的操作行为 ...
- java aop注解日志记录_springMVC自定义注解,用AOP来实现日志记录的方法
需求背景 最近的一个项目,在项目基本完工的阶段,客户提出要将所有业务操作的日志记录到数据库中,并且要提取一些业务的关键信息(比如交易单号)体现在日志中. 为了保证工期,在查阅了资料以后,决定用AOP+ ...
- 如何实现操作操作日志记录
如何实现操作操作日志记录 为什么要记录操作日志? 项目中的业务需求,需要针对用户的一些业务操作做操作记录, 也就是标题中的操场日志记录,最近做的项目也有这个需求, 我也是第一次写,相信有很多开发者也有 ...
- 共享服务器文件归档,如何实现共享文件访问记录方法、共享文件管理和共享文件操作日志记录.docx...
文档 文档 PAGE / NUMPAGES 文档 如何实现共享文件访问记录方法.共享文件管理和共享文件操作日志记录 企业单位,办公都有应用到共享文件,经常会在文件共享服务器上存储单位一些重要的共享文件 ...
- Django中间件实现操作日志记录
Django中间件实现操作日志 本文通过Django中间件的流程,实现操作日志记录的功能,模块化.拿来即用. 功能描述:通过中间件记录 请求时间.操作用户.请求URL.请求方法.请求IP.请求参数和响 ...
- qt 历史记录控件_基于Qt图形界面软件的操作日志记录方法及系统_2015106293015_说明书_专利查询_专利网_钻瓜专利网...
技术领域 本发明涉及一种软件系统的日志记录技术,特别涉及一种基于Qt图形界面软件的操作日志记录方法及系统. 背景技术 软件操作日志是记录用户在使用软件的过程中,通过鼠标和键盘在操作界面上执行的点击和输 ...
- MySQL的操作日志记录
如何配置mysql数据库的操作日志及如何查看mysql的操作日志记录 MySQL的几种操作日志 1.错误日志 log_error (主要是记录启动.运行.停止mysql时出现的致命问题,系统级别的错误 ...
最新文章
- 机器学习入门:一文让你快速了解机器学习
- 人均 11878 元,2020 年研发岗年终奖最高!技术、产品岗均榜上有名
- C++ string流
- httpd服务器常见漏洞修复,网站安全监测以及漏洞修复过程
- 缓存模式以及缓存的数据一致性
- cout的输出格式初探
- 转 知道这20个正则表达式,能让你少写1,000行代码
- elasticsearch 数据类型_基于 MySQL Binlog 的 Elasticsearch 数据同步实践
- 批处理bat命令--获取当前盘符和当前目录和上级目录
- mongodb - 查看正在执行的操作
- 自定义ClassLoader和双亲委派机制
- Zipkin分布式任务追踪
- hive练习之join操作
- vscode 设置关键字高亮显示
- 十大经典排序算法(动图演示)
- 文件下载触发的DDE注入
- 【高仿微信系列】微信录制小视频
- _WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h
- aix安装bff_AIX程序打包
- 《一斛珠·元夜月蚀》
热门文章
- css取第一个孩子,CSS:第n个孩子():后(css :nth-child() :after)
- http://www.msftconnecttest.com/redirect找不到应用程序解决办法
- android notifydatasetchanged 刷新错误,android – notifyDataSetChanged()不刷新可扩展列表视图...
- 黑马程序员 浅谈封装的具体实现的几种例子
- 教师能力发展模型【职业生涯规划的好,可以用卖白菜的努力,挣卖白粉的钱】...
- 【AI100篇经典论文】Do ImageNet Classifiers Generalize to ImageNet
- linux应用端口的网络超时,Linux服务器可以ping,但是telnet端口超时,网站wget超时,访问超时的解决办法...
- vulnhub-The Planets: Mercury
- 实验1--串口通信小试
- iOS Tabbar凸起效果