2019独角兽企业重金招聘Python工程师标准>>>

这里基于struts2的拦截器来实现。

使用struts2拦截器拦截所有或者指定的请求,对用户操作过程中的:操作用户,操作时间,操作位置,操作结果,操作用时等信息的获取以及储存,方便将来数据的查询和显示。

操作日志信息

日志信息表的建表语句:

CREATE TABLE `audit_log` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',`user_id` bigint(20) DEFAULT NULL COMMENT '用户id',`user_name` varchar(50) DEFAULT NULL COMMENT '用户名/账号',`ip` varchar(30) DEFAULT NULL COMMENT '用户IP',`start_time` bigint(20) DEFAULT NULL COMMENT '开始时间',`end_time` bigint(20) DEFAULT NULL COMMENT '结束时间',`module` varchar(200) DEFAULT NULL COMMENT '模块描述',`function` varchar(200) DEFAULT NULL COMMENT '功能描述',`clazz` varchar(255) DEFAULT NULL COMMENT '所在类',`method` varchar(100) DEFAULT NULL COMMENT 'Action方法名',`result` varchar(255) DEFAULT NULL COMMENT '返回',`use_time` bigint(20) DEFAULT NULL COMMENT '使用时间:毫秒',`create_time` datetime DEFAULT NULL COMMENT '创建时间',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;

贴出对应的需要持久化的日志信息类:

package com.zkbc.core.dao.model;import java.io.Serializable;
import java.util.Date;public class AuditLog implements Serializable{/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.id** @mbggenerated Thu May 03 16:04:24 CST 2018*/private Long id;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.user_id** @mbggenerated Thu May 03 16:04:24 CST 2018*/private Long userId;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.user_name** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String userName;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.ip** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String ip;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.start_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/private Long startTime;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.end_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/private Long endTime;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.module** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String module;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.function** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String function;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.clazz** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String clazz;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.method** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String method;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.result** @mbggenerated Thu May 03 16:04:24 CST 2018*/private String result;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.use_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/private Long useTime;/*** This field was generated by MyBatis Generator.* This field corresponds to the database column audit_log.create_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/private Date createTime;/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.id** @return the value of audit_log.id** @mbggenerated Thu May 03 16:04:24 CST 2018*/public Long getId() {return id;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.id** @param id the value for audit_log.id** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setId(Long id) {this.id = id;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.user_id** @return the value of audit_log.user_id** @mbggenerated Thu May 03 16:04:24 CST 2018*/public Long getUserId() {return userId;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.user_id** @param userId the value for audit_log.user_id** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setUserId(Long userId) {this.userId = userId;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.user_name** @return the value of audit_log.user_name** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getUserName() {return userName;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.user_name** @param userName the value for audit_log.user_name** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setUserName(String userName) {this.userName = userName == null ? null : userName.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.ip** @return the value of audit_log.ip** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getIp() {return ip;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.ip** @param ip the value for audit_log.ip** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setIp(String ip) {this.ip = ip == null ? null : ip.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.start_time** @return the value of audit_log.start_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public Long getStartTime() {return startTime;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.start_time** @param startTime the value for audit_log.start_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setStartTime(Long startTime) {this.startTime = startTime;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.end_time** @return the value of audit_log.end_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public Long getEndTime() {return endTime;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.end_time** @param endTime the value for audit_log.end_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setEndTime(Long endTime) {this.endTime = endTime;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.module** @return the value of audit_log.module** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getModule() {return module;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.module** @param module the value for audit_log.module** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setModule(String module) {this.module = module == null ? null : module.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.function** @return the value of audit_log.function** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getFunction() {return function;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.function** @param function the value for audit_log.function** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setFunction(String function) {this.function = function == null ? null : function.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.clazz** @return the value of audit_log.clazz** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getClazz() {return clazz;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.clazz** @param clazz the value for audit_log.clazz** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setClazz(String clazz) {this.clazz = clazz == null ? null : clazz.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.method** @return the value of audit_log.method** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getMethod() {return method;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.method** @param method the value for audit_log.method** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setMethod(String method) {this.method = method == null ? null : method.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.result** @return the value of audit_log.result** @mbggenerated Thu May 03 16:04:24 CST 2018*/public String getResult() {return result;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.result** @param result the value for audit_log.result** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setResult(String result) {this.result = result == null ? null : result.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.use_time** @return the value of audit_log.use_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public Long getUseTime() {return useTime;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.use_time** @param useTime the value for audit_log.use_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setUseTime(Long useTime) {this.useTime = useTime;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column audit_log.create_time** @return the value of audit_log.create_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public Date getCreateTime() {return createTime;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column audit_log.create_time** @param createTime the value for audit_log.create_time** @mbggenerated Thu May 03 16:04:24 CST 2018*/public void setCreateTime(Date createTime) {this.createTime = createTime;}/*** This method was generated by MyBatis Generator.* This method corresponds to the database table audit_log** @mbggenerated Thu May 03 16:04:24 CST 2018*/@Overridepublic String toString() {StringBuilder sb = new StringBuilder();sb.append(getClass().getSimpleName());sb.append(" [");sb.append("Hash = ").append(hashCode());sb.append(", id=").append(id);sb.append(", userId=").append(userId);sb.append(", userName=").append(userName);sb.append(", ip=").append(ip);sb.append(", startTime=").append(startTime);sb.append(", endTime=").append(endTime);sb.append(", module=").append(module);sb.append(", function=").append(function);sb.append(", clazz=").append(clazz);sb.append(", method=").append(method);sb.append(", result=").append(result);sb.append(", useTime=").append(useTime);sb.append(", createTime=").append(createTime);sb.append("]");return sb.toString();}
}

拦截器

package com.yh.userAudit;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.zkbc.base.BeanHelper;
import com.zkbc.core.dao.model.AuditLog;
import com.zkbc.core.dao.model.User;
import com.zkbc.core.service.IAuditLogService;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.springframework.beans.factory.BeanFactory;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;/*** @Description: 用户审计数据记录拦截器* @Author: 张颖辉(yh)* @CreateDate: 2018/5/2 13:17* @UpdateUser: 张颖辉(yh)* @UpdateDate: 2018/5/2 13:17* @UpdateRemark: The modified content* @Version: 1.0*/
public class AuditLogInterceptor extends MethodFilterInterceptor {/*** @Description: 拦截器方法* @Author: 张颖辉(yh)* @Date: 2018/5/2 13:52* @param: [actioninvocation:action 调用对象]* @return: java.lang.String* @Version: 1.0*/@Overrideprotected String doIntercept(ActionInvocation actioninvocation) throws Exception {AuditLog auditLog = new AuditLog();Date startDate = new Date();String result = actioninvocation.invoke();// 递归调用拦截器/*时间相关*/auditLog.setStartTime(startDate.getTime());// 设置开始时间longDate endDate = new Date();auditLog.setEndTime(endDate.getTime());// 设置结束时间longauditLog.setCreateTime(startDate);//设置创建时间auditLog.setUseTime(endDate.getTime() - startDate.getTime());//设置用时longUser user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");auditLog.setUserId(user.getUserId().longValue());// 设置登录用户的Id,在用户登录时把id保存到session中,这里可扩展判断用户是否登录的验证和权限验证auditLog.setUserName(user.getNickName());String methodName = actioninvocation.getProxy().getMethod();if (methodName.length() > 0) {Object action = actioninvocation.getAction();Class clazz = action.getClass();/*Action 类名*/auditLog.setClazz(clazz.getName());/*模块名称 如果设置了注解则读取注解的内容*/if (clazz.isAnnotationPresent(AuditLogger.class)) {AuditLogger talClazz = (AuditLogger) clazz.getAnnotation(AuditLogger.class);if (StringUtils.isNotBlank(talClazz.module())) {auditLog.setModule(talClazz.module());}}Method method = action.getClass().getMethod(methodName, null);/*方法名称*/auditLog.setMethod(methodName);/*功能描述 如果设置了注解则读取注解的内容*/if (method.isAnnotationPresent(AuditLogger.class)) {AuditLogger alm = method.getAnnotation(AuditLogger.class);if (StringUtils.isNotBlank(alm.function())) {auditLog.setFunction(alm.function());}}String ip = ServletActionContext.getRequest().getRemoteAddr();auditLog.setIp(ip);// 记录登录的IP,这里还可以对IP进行鉴权功能(允许或限制某些IP)auditLog.setResult(result);// 记录登录时返回的结果.//System.out.println("审计日志:" + auditLog);/*审计日志持久化*/addAuditLog(actioninvocation,auditLog);}return result; // 跳转}private void addAuditLog(ActionInvocation actioninvocation, AuditLog auditLog) {//该方法自己实现,我使用的方法比较特殊,这里我就删除了。}
}

注解类

package com.yh.userAudit;import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @Description: [用户安全审计] 模块功能标识注解* @Author: 张颖辉(yh)* @CreateDate: 2018/5/2 13:48* @UpdateUser: 张颖辉(yh)* @UpdateDate: 2018/5/2 13:48* @UpdateRemark: The modified content* @Version: 1.0*/
@Retention(RetentionPolicy.RUNTIME)
@Target({java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.TYPE})
public @interface AuditLogger {/*模块*/String module() default "";/*功能*/String function() default "";
}

拦截器配置

然后将拦截器配置到 struts.xml中就可以了

1 标签<interceptors>下配置:

2 再配置拦截器栈interceptor-stack

3 将interceptor-stack配置到使用本拦截器的<package>标签下(应该是配置了登录拦截器的package)

注解的使用

上面定义的注解@AuditLogger 既可以在类上,也可以在方法上。

用在类上的时候,填入参数module=“模块描述”

用在方法上的时候,填入参数function=“功能描述”

如果没有注解,也会在数据库中保存本条操作记录,可上诉两个字段,值为null。不过即使没有注解,插入的数据中也会有访问的action类名,和方法名。所以在这里注解不是必须使用的,只是使用以后可以更方便知道用户操作的具体功能。

扩展

上面的方式是把所有用户登录后的操作保存(拦截器配置到了所有需要登录的package中)。

如果只需要记录一部分重要的操作,那么可以修改代码,只有被注释的类和方法才会在执行时持久化数据。

那么所有的重要操作必须都要加上注释,才能保证数据的完整。

转载于:https://my.oschina.net/iyinghui/blog/1806346

基于struts2拦截器实现用户操作日志记录相关推荐

  1. qt 历史记录控件_基于Qt图形界面软件的操作日志记录方法及系统_2015106293015_说明书_专利查询_专利网_钻瓜专利网...

    技术领域 本发明涉及一种软件系统的日志记录技术,特别涉及一种基于Qt图形界面软件的操作日志记录方法及系统. 背景技术 软件操作日志是记录用户在使用软件的过程中,通过鼠标和键盘在操作界面上执行的点击和输 ...

  2. 使用Spring AOP自定义注解方式实现用户操作日志记录

    1,开发环境 操作系统:Windows 7 JDK:1.8.0_161 Eclipse:Mars.2 Release (4.5.2) 2,自定义注解类UserLog @Target({ElementT ...

  3. 自定义注解妙用,一行代码搞定用户操作日志记录,你学会了吗?

    来源:https://blog.csdn.net/yjt520557/article/details/85099115 | 简介 我在使用spring完成项目的时候需要完成记录日志,我开始以为Spri ...

  4. 自定义注解妙用,一行代码搞定用户操作日志记录

    1.简介 在使用spring完成项目的时候需要完成记录日志,开始以为Spring 的AOP功能,就可以轻松解决,半个小时都不用,可是经过一番了解过后,发现一般的日志记录,只能记录一些简单的操作,例如表 ...

  5. 用户操作日志记录字段修改前后值

    你可能遇到这样的需求,要详细记录用户的操作日志,像下面这样: 用户张三将年龄从"20"改为"21" 用户张三将爱好从"篮球"改为" ...

  6. struts2拦截器理论知识

    软件设计中DRY规则,即不要写重复的代码,这样可以大大减轻后期维护的工作量!但是问题来了,重复的代码是不可避免的,比如在用户发送请求之前先判断用户是否登录,怎么办呢?于是就把这些重复代码提取出来,封装 ...

  7. mysql中用户操作日志_Mysql 纪录用户操作日志

    我们想追踪某个数据库操作记录,如想找出是谁操作了某个表(比如谁将字段名改了). 二进制日志记录了操作记录,线程号等信息,但是却没有记录用户信息,因此需要结合init-connect来实现追踪. ini ...

  8. 基于SSH2框架Struts2拦截器的登录验证实现 来自菠萝大象

    本文为菠萝大象原创,如要转载请注明出处. 通过之前的 Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解系列文章,我们已经成功将这三种框架整合到了一起, 这次大 ...

  9. Struts2 拦截器(Interceptor )原理和配置

    Struts2 拦截器 拦截器在概念上与servlet过滤器或JDK代理类相同.拦截器允许横切功能,把action以及框架分开实现.你可以使用拦截器实现以下操作: 在调用action之前提供预处理逻辑 ...

最新文章

  1. eclipse中导入web项目详细配置
  2. PouchContainer 发布 0.3.0 版本,支持 Kubernetes 拥抱 CNCF 生态
  3. Object C语法学习
  4. 在Nginx和Apache服务器配置https
  5. 分隔百度百科中的名人信息与非名人信息
  6. Linux 服务器性能参数指标总结
  7. SuperMap导航条控件设置
  8. spring配置数据源(交给spring容器完成)
  9. 子集生成 --二进制法
  10. 【进阶】 --- 多线程、多进程、异步IO实用例子
  11. WEB版一次选择多个文件进行批量上传(Plupload)的解决方案
  12. adb命令获取app布局文件xml
  13. <2021SC@SDUSC> 开源游戏引擎 Overload 代码模块分析 之 OvGame (一)—— 概况与 .rc 文件
  14. Log4j漏洞及解决方案,亲测
  15. dtu阿里云自定义透传
  16. python删除文本框内容_js清除文本框内容
  17. 爬虫爬取千万数据如何快速保存,看这里
  18. 度中心度(Degree Centrality)
  19. 图文讲解 WiFi 驱动移植过程,很肝~
  20. Pychram连接mist远程服务器踩坑指南

热门文章

  1. python的seaborn下载_Python可视化 | Seaborn包—heatmap()
  2. java 核桃的数量,[Java教程]【蓝桥杯】历届试题 核桃的数量
  3. java 0b,java 1.6.0_38-b05 vm 20.13-b02优化手记
  4. 解决样本不平衡问题的奇技淫巧 汇总
  5. svm中支持向量的理解
  6. 计算机辅助教学 林筑英,视频教学制作技巧.doc
  7. 【项目管理】接手一支技术团队,你会做些哪些事?
  8. 《系统集成项目管理工程师》必背100个知识点-31WBS的分解原则
  9. 利用pyinstaller打包Python程序为一个可执行文件
  10. el-input中设置onkeypress事件是否匹配正则表达式显示输入内容的格式