转载: http://www.cnblogs.com/guokai870510826/p/5981015.html

使用标签来设置需要的记录

实例:@ISystemLog()

  1. @Controller
  2. @RequestMapping("test")
  3. public class TestController {
  4. @RequestMapping(value = "test.do", method = RequestMethod.GET)
  5. @ISystemLog(module = "TestController",methods = "test")
  6. public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception {
  7. HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  8. return new ModelAndView("/login");
  9. }
  10. }

创建ISystemLog

  1. package com.helka.cmis.common.utils.log;
  2. import java.lang.annotation.*;
  3. @Target({ElementType.PARAMETER, ElementType.METHOD})
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. public @interface ISystemLog {
  7. String module() default "";
  8. String methods() default "";
  9. }

创建LogAopAction

  1. package com.helka.cmis.common.utils.log;
  2. import com.helka.cmis.common.utils.LogEntity;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.Signature;
  5. import org.aspectj.lang.annotation.After;
  6. import org.aspectj.lang.annotation.AfterReturning;
  7. import org.aspectj.lang.annotation.AfterThrowing;
  8. import org.aspectj.lang.annotation.Around;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.aspectj.lang.reflect.MethodSignature;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import org.springframework.web.context.request.ServletWebRequest;
  17. import javax.annotation.Resource;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.lang.reflect.Method;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. @Aspect
  24. public class LogAopAction {
  25. // @Resource(name="logService")
  26. // private LogServiceImpl logservice;
  27. //配置接入点,如果不知道怎么配置,可以百度一下规则
  28. @Pointcut("execution(* com.kintech.*.controller..*.*(..))")
  29. private void controllerAspect(){}
  30. // @Autowired
  31. // private HttpServletRequest request;
  32. //@Around("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))")
  33. @Around("controllerAspect()")
  34. public Object around(ProceedingJoinPoint pjp) throws Throwable {
  35. //常见日志实体对象
  36. LogEntity log = new LogEntity();
  37. //获取登录用户账户
  38. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  39. ServletWebRequest servletWebRequest=new ServletWebRequest(request);
  40. HttpServletResponse response=servletWebRequest.getResponse();
  41. String name = (String) request.getSession().getAttribute("USER_ID");
  42. log.setAccount(name);
  43. //获取系统时间
  44. String time = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
  45. log.setCreatetime(time);
  46. String ip = request.getHeader("X-Real-IP");
  47. log.setIp(ip);
  48. long start = System.currentTimeMillis();
  49. // 拦截的实体类,就是当前正在执行的controller
  50. Object target = pjp.getTarget();
  51. // 拦截的方法名称。当前正在执行的方法
  52. String methodName = pjp.getSignature().getName();
  53. // 拦截的方法参数
  54. Object[] args = pjp.getArgs();
  55. // 拦截的放参数类型
  56. Signature sig = pjp.getSignature();
  57. MethodSignature msig = null;
  58. if (!(sig instanceof MethodSignature)) {
  59. throw new IllegalArgumentException("该注解只能用于方法");
  60. }
  61. msig = (MethodSignature) sig;
  62. Class[] parameterTypes = msig.getMethod().getParameterTypes();
  63. Object object = null;
  64. // 获得被拦截的方法
  65. Method method = null;
  66. try {
  67. method = target.getClass().getMethod(methodName, parameterTypes);
  68. } catch (NoSuchMethodException e1) {
  69. // TODO Auto-generated catch block
  70. e1.printStackTrace();
  71. } catch (SecurityException e1) {
  72. // TODO Auto-generated catch block
  73. e1.printStackTrace();
  74. }
  75. if (null != method) {
  76. // 判断是否包含自定义的注解,说明一下这里的SystemLog就是我自己自定义的注解
  77. if (method.isAnnotationPresent(ISystemLog.class)) {
  78. ISystemLog systemlog = method.getAnnotation(ISystemLog.class);
  79. log.setModule(systemlog.module());
  80. log.setMethod(systemlog.methods());
  81. try {
  82. object = pjp.proceed();
  83. long end = System.currentTimeMillis();
  84. //将计算好的时间保存在实体中
  85. log.setUsedtime(end-start);
  86. log.setCommit("Success!");
  87. //保存进数据库
  88. //logservice.saveLog(log);
  89. } catch (Throwable e) {
  90. // TODO Auto-generated catch block
  91. long end = System.currentTimeMillis();
  92. log.setUsedtime(end-start);
  93. log.setCommit("Failed");
  94. //logservice.saveLog(log);
  95. }
  96. } else {//没有包含注解
  97. object = pjp.proceed();
  98. }
  99. } else { //不需要拦截直接执行
  100. object = pjp.proceed();
  101. }
  102. return object;
  103. }
  104. }

创建数据库的映射实体

  1. public class LogEntity {
  2. private int id;
  3. private String account;
  4. private String module;
  5. private String method;
  6. private long usedtime;
  7. private String ip;
  8. private String createtime;
  9. private String commit;
  10. public int getId() {
  11. return id;
  12. }
  13. public void setId(int id) {
  14. this.id = id;
  15. }
  16. public String getAccount() {
  17. return account;
  18. }
  19. public void setAccount(String account) {
  20. this.account = account;
  21. }
  22. public String getModule() {
  23. return module;
  24. }
  25. public void setModule(String module) {
  26. this.module = module;
  27. }
  28. public String getMethod() {
  29. return method;
  30. }
  31. public void setMethod(String method) {
  32. this.method = method;
  33. }
  34. public long getUsedtime() {
  35. return usedtime;
  36. }
  37. public void setUsedtime(long usedtime) {
  38. this.usedtime = usedtime;
  39. }
  40. public String getIp() {
  41. return ip;
  42. }
  43. public void setIp(String ip) {
  44. this.ip = ip;
  45. }
  46. public String getCreatetime() {
  47. return createtime;
  48. }
  49. public void setCreatetime(String createtime) {
  50. this.createtime = createtime;
  51. }
  52. public String getCommit() {
  53. return commit;
  54. }
  55. public void setCommit(String commit) {
  56. this.commit = commit;
  57. }
  58. }

配置文件:

springmvc-servlet.xml

  1. <aop:aspectj-autoproxy proxy-target-class="true" />
  2. <bean id="logAopAction" class="com.helka.cmis.common.utils.log.LogAopAction" />

aop 记录用户操作(一)的更多相关文章

  1. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  2. SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  3. RabbitMQ实战场景(一):异步记录用户操作日志

    传统的项目开发中业务流程以串行方式,执行了模块1—>模块2–>模块3 而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点: (1)整个流程的执行响应等待时间比较长; ...

  4. Spring AOP使用注解记录用户操作日志

    最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...

  5. 使用aop记录数据库操作的执行时间

    在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志. 首先我们可以写一个类实现MethodInterceptor接口: import org.aopalliance ...

  6. springAOP记录用户操作日志

    项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用springAOP来完成比较合适. 注解工具类: @Retention(RetentionPolicy.RUNTIME) @Tar ...

  7. 使用 script 命令记录用户操作行为

    Script 命令可以帮助管理员记录用户的操作行为,包括用户查看文件中的哪些具体内容,写入了哪些文件,写了些什么都能看到,比较详细的记录了用户的操作行为. 本文对此进行简要说明. 1.添加日志记录 e ...

  8. 记录用户操作历史命令history

    我们知道可以使用history命令,查看自己的操作记录,但如果你是root用户,如何查看其它用户的操作记录呢?   其实history命令只是把当前用户目录下的~/.bash_History文件内容列 ...

  9. linux系统监控:记录用户操作轨迹,谁动过服务器

    1.前言 我们在实际工作当中,都碰到过误操作.误删除.误修改过配置文件等等事件.对于没有堡垒机的公司来说,要在linux系统上深究到底谁做过配置文件的修改.做过误删除是很头疼的事情,特别是遇到删库跑路 ...

随机推荐

  1. SkylineGlobe6.5版本,在矿山、石油、天然气等能源行业的最新应用DEMO演示

    SkylineGlobe6.5版本,在矿山.石油.天然气等能源行业的最新应用DEMO演示: http://v.youku.com/v_show/id_XNTc3Njc1OTEy.html 一个Pres ...

  2. C# DllImport 相对路径无法找到dll

    原文:C# DllImport 相对路径无法找到dll 如题,近期在开发过程中,需要调用C++的库,一般来说,使用下面的方法即可正常调用: [DllImport("hci_sys.dll&q ...

  3. 校内模拟赛 虫洞(by NiroBC)

    题意: n个点m条边的有向图,每一天每条边存在的概率都是p,在最优策略下,询问从1到n的期望天数. 分析: dijkstra. 每次一定会优先选dp最小的后继走,如果这条边不存在,选次小的,以此类推. ...

  4. item 3: 理解decltype

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 decltype是一个奇怪的东西.给出一个名字或者一个表达式,de ...

  5. [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

    UWP的UI主要由布局容器和内容控件(ContentControl)组成.布局容器是指Grid.StackPanel等继承自Panel,可以拥有多个子元素的类.与此相对,ContentControl则 ...

  6. width,height为多少px时,A4纸打印时刚好一页?

    计算方式一般的分辨率为XX像素/英寸,其中一英寸为25.4毫米.所以一毫米的像素数就为XX/25.4.现在的工作就是求XX的值了,把XX的值求出来以后,直接用XX/25.4 * 210就得到A4纸的像 ...

  7. Docker阿里云镜像加速

    登录阿里云docker registry sudo docker login --username=zhangsan@163.com registry.cn-hangzhou.aliyuncs.com ...

  8. SpringBoot笔记--Jackson

    SpringUtil.getBean<GenericConversionService>().addConverter(Date2LocalDateTimeConverter()) var ...

  9. 运维中的日志切割操作梳理(Logrotate/python/shell脚本实现)

    对于Linux系统安全来说,日志文件是极其重要的工具.不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分Nginx日志之类的CRON脚本,大家似乎遗忘了Logrotate,争相发明自己的轮 ...

  10. 浅谈JS的作用域链(一)

    JS的执行环境 执行环境(Execution context,EC)或执行上下文,是JS中一个极为重要的概念. 在JavaScript中有三种代码运行环境: Global Code JavaScrip ...