1: an operation log custom annotations
/ ** * operation log annotation * / @Target ({ElementType.TYPE, ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) public @interface OperateLog {
/ ** * Module Name * * @return * / String Module1 () ;
/ * * name operation * * @return * / String function ();
/ ** * filter sensitive words * * @return * / Class <? OperateLogHandler the extends> Handler () default OperateLogHandler.class;
} |
2: Operation log AOP
/ ** * Operation Log AOP * / SLF4J @ @Aspect @Component public class OperateLogAspect {
Private static String OPERATE_LOG_ID Final = "operateLogId";
@Autowired Private SysOperateLogService sysOperateLogService;
/ ** * tangent point: @annotation indicates hold "cn.XXXX.annotation.OperateLog" annotation method the notification will be performed aop * / @Pointcut ( "Annotation @ (cn.xiweiai.dc.web.admin.annotation.OperateLog)") public void the pointcut () { // }
/ ** * pre-defined notifications * / @Before ( " the pointcut () ") public void before (the JoinPoint Point) { HttpServletRequest request = ServletRequestAttributesUtils.getHttpServletRequest(); SysOperateLog operateLog = new SysOperateLog(); //组装操作日志 if (CurrentUserUtils.isLogin()) { operateLog.setUserId(CurrentUserUtils.getCurrentUserId()); } OperateLog annotation = getOperateLog(point); operateLog.setModuleName(annotation.module()); operateLog.setFunctionName(annotation.function()); operateLog.setReqUrl(request.getRequestURI()); operateLog.setIp(request.getRemoteAddr()); operateLog.setReqBody(getReqBody(point)); operateLog.setCreateTime(new Date()); //保存操作日志 sysOperateLogService.add (operateLog); // log into the article id request field, to prepare for subsequent use request.setAttribute (OPERATE_LOG_ID, operateLog.getId ()); }
/ ** * returns the notification post * * @ Result param * / @AfterReturning ( the pointcut = "the pointcut ()", returning = "Result" ) public void the afterReturning (Result Object) { the HttpServletRequest ServletRequestAttributesUtils.getHttpServletRequest Request = (); Long operateLogId = (Long) the request.getAttribute (OPERATE_LOG_ID) ; IF (operateLogId == null) { log.error ( "operateLogId empty"); return; } String respBody = null; if (result != null) { try { respBody = ObjectMapperUtils.writeValueAsString(result); } catch (JsonParseException e) { log.error(e.getMessage(), e); } } SysOperateLog operateLog = new SysOperateLog(); operateLog.setId(operateLogId); operateLog.setRespBody(respBody); operateLog.setUpdateTime(new Date());
if (CurrentUserUtils.isLogin()) { operateLog.setUserId(CurrentUserUtils.getCurrentUserId()); } //后置通知:更新日志 sysOperateLogService.update(operateLog); }
OperateLog getOperateLog Private (the JoinPoint Point) { MethodSignature Signature = (MethodSignature) point.getSignature (); Method, Method signature.getMethod = (); return method.getAnnotation (OperateLog.class); } / ** * parameter request parameter acquisition request body after the acquisition, will become empty null, encapsulated request parameters, can be used to ensure that the subsequent acquired. * * @Param Point * @return * / Private String getReqBody (the JoinPoint Point) { String reqBody = null; the try { the Map <String, Object> WebUtils.getParams the params = (); IF (CollectionUtils.isEmpty (the params)) { return null; } Class<? extends OperateLogHandler> clazz = getOperateLog(point).handler(); if (clazz != null && !clazz.isAssignableFrom(OperateLogHandler.class)) { reqBody = clazz.newInstance().handler(params); } else { reqBody = ObjectMapperUtils.writeValueAsString(params); } } catch (Exception e) { log.error(e.getMessage(), e); } return reqBody; } } |
3: Log class
/** * 系统操作日志表 * @date 2019-08-27 */ @Data @Entity @Table(name = "dc_sys_operate_log") public class SysOperateLog {
@Id @GeneratedValue(generator = "JDBC") private Long id;
/** * 组织id */ private Long userId;
/** * 模块名称 */ private String moduleName;
/** * 功能名称 */ private String functionName;
/** * 请求url */ private String reqUrl;
/** * 登录名 */ private String ip;
/** * 请求body(包含url参数) */ private String reqBody;
/** * 响应body */ private String respBody;
/** * 密码 */ private String detail;
/** * 创建时间 */ private Date createTime;
/** * 更新时间 */ private Date updateTime;
} |
4:调用AOP
/** * 系统用户控制器(成员管理) */ @Slf4j @RestController @RequestMapping(value = "/user", produces = APPLICATION_JSON_UTF8_VALUE) public class SysUserController {
@Autowired private SysUserService sysUserService;
/** * 系统用户列表 * * @param dto * @return */ @OperateLog(module = "用户管理", function = "用户列表") @RequiresPermissions(USER_LIST) @PostMapping(value = "/list") public DabResponse list(@Valid @RequestBody QuerySysUserListReqDTO dto) {
return success(sysUserService.getQuerySysUserListRespDTO(dto)); }
/** * 修改用户信息 * * @param dto * @return */ @OperateLog(module = "用户管理", function = "修改用户信息") @RequiresPermissions(USER_LIST) @PostMapping(value = "/update") public DabResponse update(@Valid @RequestBody SysUserUpdateDTO dto) { sysUserService.update(dto); return success(); }
/** * 修改用户状态 * * @param dto * @return */ @OperateLog(module = "用户管理", function = "修改用户状态") @RequiresPermissions(USER_LIST) @PostMapping(value = "/update/status") public DabResponse updateStatus(@Valid @RequestBody SysUserUpdateStatusDTO dto) { sysUserService.updateStatus(dto); return success(); }
} |