ssm 项目记录用户操作日志和异常日志
借助网上参考的内容,写出自己记录操作日志的心得!!
我用的是ssm项目使用aop记录日志;这里用到了aop的切点 和 自定义注解方式;
1、建好数据表:
数据库记录的字段有: 日志id 、操作人、操作人IP、操作时间、操作方法、操作哪个控制层或者服务层、操作说明(记录用户操作的详情说明)、类型、异常信息
2、在spring重配置如下:
因为在spring里我配置了记录服务层的切点; 所以我在spring-mvc 里面 重新配置了一个可以扫描控制层的切点,使用的是cglib 代理:
<!-- 启动AOP AspectJ注解自动代理 -->
<aop:aspectj-autoproxy />
<!-- 通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller -->
<aop:config proxy-target-class="true"></aop:config>
3、接下来写自定义的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface ArchivesLog { /**
* 操作说明
*/
public String operteContent() default ""; }
4、拿到切点的内容,并且在此存库
package com.zhkj.jtpdms.log; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import com.zhkj.jtpdms.entity.system.LogInfo;
import com.zhkj.jtpdms.entity.system.User;
import com.zhkj.jtpdms.service.system.LogInfoService;
import com.zhkj.jtpdms.util.GlobalConstant; /**
* 操作日志类
*
* @author MEIM
*
*/ @Aspect
@Component
public class ArchivesLogAspect {
@Autowired
private LogInfoService loginfoService; private static final Logger logger = LoggerFactory.getLogger(ArchivesLog.class); @Pointcut("@annotation(ArchivesLog)")
public void controllerAspect() {
//System.out.println("切入点...");
} /**
* 方法调用后触发 , 记录正常操作
*
* @param joinPoint
* @throws ClassNotFoundException
*/
@AfterReturning("controllerAspect()")
public void after(JoinPoint joinPoint) throws ClassNotFoundException {
// 用户id
int userId = getUSerMsg().getId();
// 用户IP
String ip = getUSerMsg().getLoginIp();
// 控制器名
String targetName = getMethodDesc(joinPoint).getController();
// 方法名
String methodName = getMethodDesc(joinPoint).getMethod();
// 操作说明
String operteContent = getMethodDesc(joinPoint).getOperateContent();
LogInfo logInfo = new LogInfo();
logInfo.setUserId(userId);
logInfo.setIp(ip);
logInfo.setOperateContent(operteContent);
logInfo.setMethod(methodName);
logInfo.setController(targetName);
loginfoService.insertLog(logInfo);
} /**
* 发生异常,走此方法
* @param joinPoint
* @param e
*/
@AfterThrowing(pointcut = "controllerAspect()", throwing = "e")
public void AfterThrowing(JoinPoint joinPoint, Throwable e) {
try {
// 用户id
int userId = getUSerMsg().getId();
// 用户IP
String ip = getUSerMsg().getLoginIp();
// 控制器名
String targetName = getMethodDesc(joinPoint).getController();
// 方法名
String methodName = getMethodDesc(joinPoint).getMethod();
// 操作说明
String operteContent = getMethodDesc(joinPoint).getOperateContent();
LogInfo logInfo = new LogInfo();
String exMsg = e.getCause().toString();
if (exMsg != null) {
int type = 2;
logInfo.setUserId(userId);
logInfo.setIp(ip);
logInfo.setOperateContent(operteContent);
logInfo.setMethod(methodName);
logInfo.setController(targetName);
logInfo.setType(type);
logInfo.setExMsg(exMsg);
loginfoService.insertLog(logInfo);
}
} catch (Exception e1) {
logger.error(e1.getMessage());
}
} /**
* 获取 注解中对方法的描述
*
* @return
* @throws ClassNotFoundException
*/
public static LogInfo getMethodDesc(JoinPoint joinPoint) throws ClassNotFoundException {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String operteContent = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
// 操作说明
operteContent = method.getAnnotation(ArchivesLog.class).operteContent();
break;
}
}
}
LogInfo logInfo = new LogInfo();
logInfo.setController(targetName);
logInfo.setMethod(methodName);
logInfo.setOperateContent(operteContent);
return logInfo;
} /**
* 得到用户信息
*
* @return
*/
public static User getUSerMsg() {
HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 获取session
HttpSession session = req.getSession();
User user = (User) session.getAttribute(GlobalConstant.SESSION_USER);
return user;
} }
5、在控制层/服务层加上自定义注解即可成功
@RequestMapping("/saveUser")
@ResponseBody
// 此处加上自定义注解 ,operteContent 是操作说明
@ArchivesLog(operteContent = "新增用户")
public JsonResult saveUser(User user, String check) {
//... 增加操作
}
6、查看效果如下:

7、注意事项:
需要在spring-mvc 配置aop !!!
通知的几个属性(@around , @before ,@after ,@afterRetruning,@afterThrowing)需要弄清楚!!
还有不明白之处欢迎沟通!
ssm 项目记录用户操作日志和异常日志的更多相关文章
- RabbitMQ实战场景(一):异步记录用户操作日志
传统的项目开发中业务流程以串行方式,执行了模块1—>模块2–>模块3 而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点: (1)整个流程的执行响应等待时间比较长; ...
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
- Spring AOP使用注解记录用户操作日志
最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...
- springAOP记录用户操作日志
项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用springAOP来完成比较合适. 注解工具类: @Retention(RetentionPolicy.RUNTIME) @Tar ...
- 使用 script 命令记录用户操作行为
Script 命令可以帮助管理员记录用户的操作行为,包括用户查看文件中的哪些具体内容,写入了哪些文件,写了些什么都能看到,比较详细的记录了用户的操作行为. 本文对此进行简要说明. 1.添加日志记录 e ...
- linux系统监控:记录用户操作轨迹,谁动过服务器
1.前言 我们在实际工作当中,都碰到过误操作.误删除.误修改过配置文件等等事件.对于没有堡垒机的公司来说,要在linux系统上深究到底谁做过配置文件的修改.做过误删除是很头疼的事情,特别是遇到删库跑路 ...
- 记录用户操作历史命令history
我们知道可以使用history命令,查看自己的操作记录,但如果你是root用户,如何查看其它用户的操作记录呢? 其实history命令只是把当前用户目录下的~/.bash_History文件内容列 ...
- 一次SSM项目记录
1.控制台输入 mvn archetype:generate -DgroupId=com.yjdev -DartifactId=myzone -DarchetypeArtifactId=maven-a ...
- linux 记录用户操作日志
将以下加入到/etc/profile 最后 history USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]/ ...
随机推荐
- JavaScript常用数组操作方法,包含ES6方法
一.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2,3]; var arr2 = [4,5]; ...
- app后端设计(php)
来源:http://blog.csdn.net/column/details/mobilebackend.html?page=1 做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经 ...
- Python学习笔记【1】
1.%r和%s的区别 (1)stackflow 上面的一个解答 (2) x = "There are %d types of people." %10 binary = " ...
- Python绘制拓扑图(无向图)、有向图、多重图。最短路径计算
前言: 数学中,“图论”研究的是定点和边组成的图形. 计算机中,“网络拓扑”是数学概念中“图”的一个子集.因此,计算机网络拓扑图也可以由节点(即顶点)和链路(即边)来进行定义和绘制. 延伸: 无向图 ...
- p5.BTC-网络
Bitcoin工作在应用层,网络层是P2P . Bitcoin网络通信的设计原则是 simple robust ,but not efficient. 每个节点维护一个邻居节点的集合,消息传播采取 ...
- SELinux 权限设置
SELinux 权限设置 一.SELinux简介 SELinux全称是Security Enhanced Linux,由美国国家安全部(National Security Agency)领导开发的GP ...
- linux 进程管理与调度(一)
进程结构 进程在内核的源代码中以结构体表示,篇幅很长,在此列举一小段关键代码,可以发现是个双向链表,具体的可以在内核目录下找一个叫"sched.h"的头文件. struct tas ...
- https跳http
listen 443 ssl;rewrite ^ http://$http_host$request_uri? permanent;
- linux 的常用命令(1)
1.关于ls [选项][目录名] -a 列出包括.a开头的隐藏文件的所有文件-A 通-a,但不列出"."和".."-l 列出文件的详细信息-c 根据ct ...
- Java中处理接口返回base64编码的图片数据
在做接口测试的时候,某些接口返回的content是一大段加密文字.这种情况下,有可能是返回的图片加密数据,需要将这些数据转换成图片进行保存查看. 例如: 这里,可以看到Content对应的键值开头有“ ...