aop记录用户操作
来源:互联网 发布:淘宝换货需要先签收吗 编辑:程序博客网 时间:2024/06/06 06:58
转载: 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)@Documentedpublic @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;@Aspectpublic 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" />
阅读全文
0 0
- aop记录用户操作
- AOP实现用户操作日志记录
- 使用Spring AOP使用注解记录用户操作日志
- spring aop 实现用户操作日志记录功能
- springMVC +Mybatis +spring aop 实现用户系统操作日志记录
- Spring自定义注解+Aop记录用户操作日志
- Spring—AOP实践,利用切面记录用户操作日志
- spring Aop 之用户操作日志以及异常处理日志记录
- spring Aop 之用户操作日志以及异常处理日志记录
- spring mvc +spring aop结合注解的 用户操作日志记录
- 记录用户登录操作
- 基于aop的用户操作日志
- aop+自定义注解实现操作日志记录
- spring AOP的 操作日志记录功能
- aop+自定义注解实现操作日志记录
- spring的AOP实现记录操作日志
- Spring AOP 自定义注解记录操作日志
- Spring AOP 实现系统操作日志记录
- 地图可视化:零编程,BDP轻松制作动态轨迹地图!
- 在 gitbook 中插入 github 上的图片
- postman自动化测试
- 最意想不到的5个APP UI 设计范例
- golang chan 使用的一个坑
- aop记录用户操作
- tensorflow官方文档中文版
- Flex-缩放自适应布局
- Java web图片上传本地预览,无需后台写接口
- 会自动伸缩的文本输入框HPGrowingTextView
- 洛谷9月月赛--T2[T2]预生成密码
- 学习浅墨opencv入门教程三之综合实例犯错总结
- Math函数
- Struts2+Ajax实现检测用户名是否唯一