基于struts2拦截器实现用户操作日志记录
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拦截器实现用户操作日志记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis Mapper Demo
- 下一篇: 重磅解读 | 赵义博:量子密码的绝对安全