package com.supinfo.jieneng.aop; import java.util.Date; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import com.supinfo.core.base.ServiceManager; import com.supinfo.jieneng.domains.Admin; import com.supinfo.jieneng.domains.Log; import com.supinfo.util.DataUtil; /** * 管理员操作日志记录 * @author * */ @Aspect public class LogAspect { @Autowired private ServiceManager serviceManager; //操作 private static final String OP_UPDATE = "修改"; private static final String OP_INSERT = "添加"; private static final String OP_DELETE = "删除"; /** * 筛选方法: * */ /*后台管理添加操作*/ @Pointcut("execution(* com.supinfo.jieneng.services.*.insert*(..))") public void insertCall() { } @Pointcut("execution(* com.supinfo.jieneng.services.*.add*(..))") public void addCall() { } @Pointcut("execution(* com.supinfo.jieneng.services.*.save*(..))") public void saveCall() { } @Pointcut("( insertCall() || addCall() || saveCall() )") public void allInsertCall(){} /*后台管理修改操作*/ @Pointcut("execution(* com.supinfo.jieneng.services.*.update*(..))") public void updateCall() { } @Pointcut("execution(* com.supinfo.jieneng.services.*.modify*(..))") public void modifyCall() { } @Pointcut("( updateCall() || modifyCall() )") public void allUpdateCall() { } /*后台管理员删除*/ @Pointcut("execution(* com.supinfo.jieneng.services.*.delete*(..))") public void deleteCall() { } @Pointcut("execution(* com.supinfo.jieneng.services.*.remove*(..))") public void removeCall() { } @Pointcut("(deleteCall() || removeCall())") public void allDeleteCall() { } /** * 获取登录管理员ID * * @return */ private Long loginUserId() { if(SecurityContextHolder.getContext() == null){ return null; } if(SecurityContextHolder.getContext().getAuthentication() == null){ return null; } UserDetails userDetails = (UserDetails) SecurityContextHolder .getContext().getAuthentication().getPrincipal(); if(userDetails == null){ return null; } String userName = userDetails.getUsername(); if(DataUtil.isNullOrEmpty(userName)){ return null; } // 根据用户名获取用户ID Admin admin = serviceManager.getAdminService().findAdminByNickname(userName); if(admin == null){ return null; } return admin.getId(); } /** * 管理员修改操作日志 * @param joinPoint * @param rtv * @throws Throwable */ @AfterReturning(value="allUpdateCall()", argNames="rtv", returning="rtv") public void allUpdateCallAfterCalls(JoinPoint joinPoint, Object rtv) throws Throwable{ Long adminUserId = loginUserId(); if(adminUserId == null){//没有管理员登录 return; } //判断参数 if(joinPoint.getArgs() == null){//没有参数 return; } //获取操作方法名 String optionMethodName = joinPoint.getSignature().getName(); Log log = new Log(); //用户编号 log.setUserid(loginUserId()); log.setCreatedate(new Date()); //设置日志内容 log.setContent(DataUtil.adminOptionContent(joinPoint.getArgs(),optionMethodName)); log.setOperation(OP_UPDATE); serviceManager.getLogService().log(log); } /** * 管理员添加操作日志 * @param joinPoint * @param rtv * @throws Throwable */ @AfterReturning(value="allInsertCall()", argNames="rtv", returning="rtv") public void allInsertCallAfterCalls(JoinPoint joinPoint, Object rtv) throws Throwable{ Long adminUserId = loginUserId(); if(adminUserId == null){//没有管理员登录 return; } //判断参数 if(joinPoint.getArgs() == null){//没有参数 return; } //获取操作方法名 String optionMethodName = joinPoint.getSignature().getName(); Log log = new Log(); //用户编号 log.setUserid(loginUserId()); log.setCreatedate(new Date()); //设置日志内容 log.setContent(DataUtil.adminOptionContent(joinPoint.getArgs(),optionMethodName)); log.setOperation(OP_INSERT); serviceManager.getLogService().log(log); } /** * @Description: 管理员删除日志记录 * @方法名: allDeleteCallAfterCalls * @param : @param joinPoint * @param : @param rtv * @return void 返回类型 * @throws */ @AfterReturning(value="allDeleteCall()", argNames="rtv", returning="rtv") public void allDeleteCallAfterCalls(JoinPoint joinPoint,Object rtv) throws Throwable{ Long adminUserId = loginUserId(); if(adminUserId == null){//没有管理员登录 return; } //判断参数 if(joinPoint.getArgs() == null){//没有参数 return; } //获取操作方法名 String optionMethodName = joinPoint.getSignature().getName(); Log log = new Log(); //用户编号 log.setUserid(loginUserId()); log.setCreatedate(new Date()); //设置日志内容 log.setContent(DataUtil.adminOptionContent(joinPoint.getArgs(),optionMethodName)); log.setOperation(OP_DELETE); serviceManager.getLogService().log(log); } }
下面是通过反射 获取当前操作对象的参数值:
/** * @Description: 使用Java反射来获取被拦截方法(insert、update,delete)的参数值 * @方法名: adminOptionContent * @param : @param args 参数 * @param : @param optionMethodName 操作方法名 * @return String 返回类型 */ public static String adminOptionContent(Object[] args,String optionMethodName) throws Exception { if (args == null) { return null; } StringBuffer rs = new StringBuffer(); //判断用户是 删除 修改 添加操作类型 if (optionMethodName.startsWith("delete") || optionMethodName.startsWith("remove")) { //方法名 rs.append("删除操作方法名:" + optionMethodName); }else if (optionMethodName.startsWith("insert") || optionMethodName.startsWith("add") || optionMethodName.startsWith("save")) { rs.append("添加操作方法名:" + optionMethodName); }else if (optionMethodName.startsWith("update") || optionMethodName.startsWith("modify")) { rs.append("修改操作方法名:" + optionMethodName); } String className = null; int index = 1; // 遍历参数对象 for (Object info : args) { // 获取对象类型 className = info.getClass().getName(); className = className.substring(className.lastIndexOf(".") + 1); rs.append("[" + index + ",类型:" + className + ",值:"); // 获取对象的所有方法 Method[] methods = info.getClass().getDeclaredMethods(); // 遍历方法,判断get方法 for (Method method : methods) { String methodName = method.getName(); // 判断是不是get方法 不是get方法 if (methodName.indexOf("get") == -1) { continue; } Object rsValue = null; try { // 调用get方法,获取返回值 rsValue = method.invoke(info); // 没有返回值 if (rsValue == null) { continue; } } catch (Exception e) { continue; } // 将值加入内容中 rs.append("(" + methodName + " : " + rsValue + ")"); } rs.append("]"); index++; } return rs.toString(); }
Spring XML配置:
<!-- 加入Aspectj 注解配置启动 --> <aop:aspectj-autoproxy /> <!-- 日志 记录 AOP注入 --> <bean id="logAspect" class="com.supinfo.jieneng.aop.LogAspect"/>
相关推荐
4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、...
本资源用来展示如何使用 spring aop 进行日志记录,例子里面通过aop的配置,把产生的日志存放到当前项目的根目录下,而且对方法执行过程中的参数进行了记录,对于aop如何记录日志不清楚的同学可以看看。
swagger和spring Aop日志结合 ,swagger和spring Aop日志结合 ,swagger和spring Aop日志结合 ,
该压缩包中包含了一个myeclipse6.5下开发的JAVA基于spring实现的日志记载例子,该例子提供了接受切点参数,解析切点返回值,并且都打印出来了,有详细的文档介绍。
SpringAOP的日志拦截示例, 包含配置和代码
利用自定义注解和spring aop和java反射机制生成用户能够读懂的日志记录。如:用户张三在2013年9月27日17:00执行了用户管理模块的用户删除功能参数为(编号:123456)各位可根据需要写入数据库或者保存到文件。
使用SpringAOP完成日志记录功能的实例
内容概要: 充分利用SpringBoot AOP设计的精妙日志记录方案,实现高效的日志处理策略。此外,我们还支持通过配置文件(yml)精准控制日志开关,并定制AOP切点表达式(Pointcut)。 适用人群: 针对资深Java开发者、...
结合logback和切面对参数名和参数值,方法运行时间,方法返回值记录日志,pom中需要添加ogback和切面依赖。
元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运维提供支持 日志记录尽可能减少性能影响
Flex整合Spring实现aop日志管理 实现记录操作的日志
时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版。感谢各位同道的支持!
Spring之AOP在鉴权和日志记录中的应用
2.需要记录异常日志时没有记录,或者异常在不同的地方重复记录,使得排错调试不方便 3.处理日志时,需要在每一个try-catch块包含一些处理代码,有时候异常处理的代码比正常执行代码还多,污染正常执行代码。 4.同样...
用spring AOP做的最简单的日志记录小例子,特别简单,可直接运行。
spring.net结合三层AOP异常日志记录功能
struts2+spring2.5用AOP记录操作日志,带自定义参数aop的例子
使用Spring的AOP技术,实现日志记录功能。记录如下信息: 在哪个时间访问了哪个方法,方法传入了哪些参数
spring aop记录日志
Spring mvc mybatis plus 实现AOP 切面日志系统,带有数据库。可以自行拓展