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

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

实例:@ISystemLog()

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

创建ISystemLog

package com.helka.cmis.common.utils.log;

import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ISystemLog {
String module() default "";
String methods() default "";
}

创建LogAopAction

package com.helka.cmis.common.utils.log;

import com.helka.cmis.common.utils.LogEntity;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date; @Aspect
public class LogAopAction { // @Resource(name="logService")
// private LogServiceImpl logservice; //配置接入点,如果不知道怎么配置,可以百度一下规则
@Pointcut("execution(* com.kintech.*.controller..*.*(..))")
private void controllerAspect(){}
// @Autowired
// private HttpServletRequest request; //@Around("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))")
@Around("controllerAspect()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
//常见日志实体对象
LogEntity log = new LogEntity();
//获取登录用户账户
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse(); String name = (String) request.getSession().getAttribute("USER_ID");
log.setAccount(name);
//获取系统时间
String time = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
log.setCreatetime(time); String ip = request.getHeader("X-Real-IP");
log.setIp(ip); long start = System.currentTimeMillis();
// 拦截的实体类,就是当前正在执行的controller
Object target = pjp.getTarget();
// 拦截的方法名称。当前正在执行的方法
String methodName = pjp.getSignature().getName();
// 拦截的方法参数
Object[] args = pjp.getArgs();
// 拦截的放参数类型
Signature sig = pjp.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Class[] parameterTypes = msig.getMethod().getParameterTypes(); Object object = null;
// 获得被拦截的方法
Method method = null;
try {
method = target.getClass().getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (null != method) {
// 判断是否包含自定义的注解,说明一下这里的SystemLog就是我自己自定义的注解
if (method.isAnnotationPresent(ISystemLog.class)) {
ISystemLog systemlog = method.getAnnotation(ISystemLog.class);
log.setModule(systemlog.module());
log.setMethod(systemlog.methods());
try {
object = pjp.proceed();
long end = System.currentTimeMillis();
//将计算好的时间保存在实体中
log.setUsedtime(end-start);
log.setCommit("Success!");
//保存进数据库
//logservice.saveLog(log);
} catch (Throwable e) {
// TODO Auto-generated catch block
long end = System.currentTimeMillis();
log.setUsedtime(end-start);
log.setCommit("Failed");
//logservice.saveLog(log);
}
} else {//没有包含注解
object = pjp.proceed();
}
} else { //不需要拦截直接执行
object = pjp.proceed();
}
return object;
}
}

创建数据库的映射实体

public class LogEntity {

    private int id;
private String account;
private String module;
private String method;
private long usedtime;
private String ip;
private String createtime;
private String commit; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getModule() {
return module;
} public void setModule(String module) {
this.module = module;
} public String getMethod() {
return method;
} public void setMethod(String method) {
this.method = method;
} public long getUsedtime() {
return usedtime;
} public void setUsedtime(long usedtime) {
this.usedtime = usedtime;
} public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public String getCreatetime() {
return createtime;
} public void setCreatetime(String createtime) {
this.createtime = createtime;
} public String getCommit() {
return commit;
} public void setCommit(String commit) {
this.commit = commit;
} }

配置文件:

springmvc-servlet.xml

<aop:aspectj-autoproxy proxy-target-class="true" />
<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. 监控 -- kubernetes -- prometheus

    1.但是Heapster无法做Kubernetes下应用的监控.现在,Heapster作为Kubernetes下的开源监控解决方案已经被其弃用,Prometheus成为Kubernetes官方推荐的监 ...

  2. [01-01] 示例:用Java爬取新闻

    1.分析url <空港双流>数字报刊,访问地址为:http://epaper.slnews.net.cn,现在为了抓取每篇新闻的网页内容. 在浏览器访问该链接后,发现链接出现了变化,看样子 ...

  3. Linux系列教程(七)——Linux常用命令之帮助和用户管理命令

    上篇博客我们介绍了Linux文件搜索命令,其中find是用的最多的也是功能最强大的文件或目录搜索命令,和另一个搜索命令locate的区别是,find命令是全盘搜索,刚创建的文件也能搜索的到,而loca ...

  4. NOIP2002-2017提高组题解

    给个人认为比较难的题目打上'*' NOIP2002(clear) //一个很吼的贪心题,将平均数减掉之后从左往右将影响消除 #include<bits/stdc++.h> using na ...

  5. (转)Ubuntu init启动流程分析

    原文 upstart homepage 现行的Linux distros主流的有两种init方式:一种是广为流传的System V initialization,它来源于Unix并且至今仍被各种Lin ...

  6. python升级后带来的几个小问题

    1)python升级带来的yum异常:File "/usr/bin/yum", line 30 原因:这是因为yum采用Python作为命令解释器,这可以从/usr/bin/yum ...

  7. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-A-Single Wildcard Pattern Matching

    #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...

  8. vue侧边栏导航和右边内容一样高

    vue侧边栏导航和右边内容一样高吗? 失败了,最后用做导航和上导航 定位, 右内容类似滚动条 效果: 直接把top导航和左侧导航栏display:flxed定位左边,右边内容left: top

  9. [2017BUAA软件工程]第0次作业

    第一部分:结缘计算机 1. 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?(必答) 选择计算机专业的一个重要原因是因为计算机专业的就业前景好,由于计算机本身具有的各种优点,现在几乎所有的 ...

  10. Golang的格式化输出fmt.Printf

    本文来源:Go by example. Golang的格式化输出 和 C语言的标准输出基本一样,但是增加了一些针对Golang语言的特有数据结构的格式化输出方式. 一下就是实例: package ma ...