注解
1、定义注解
/** * @author * @create 2020/7/22 11:30 */ @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ActionRecordLogAspect { /** * 操作描述 */ String descriptions() default ""; /** * 日志类型 */ String logType() default "GENERAL"; /** * 忽略参数 */ String ignoreParams() default ""; }
2、定义注解执行逻辑
/** * @Date 2020/7/22 17:13 * @Description: */ @Aspect @Component public class ActionLogAspect { private static final Logger logger = LoggerFactory.getLogger(ActionLogAspect.class); private static final String IGNORE_METHOD = "GET"; @Reference(async = true) private RecordDubboService recordDubboService;
// 环绕,@annotation里要填全路径,所有有该注解的方法都会切入
// @Before 方法执行前进入切面
// @After 方法执行后进入切面 @Around(value = "@annotation(com.***.ActionRecordLogAspect)") public Object processAuthority(ProceedingJoinPoint joinPoint) throws Throwable { Object result = joinPoint.proceed(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String requestMethod = request.getMethod(); String remoteIP = IpUtils.getRemoteIP(request); String url = request.getRequestURL().toString(); Long optUserId = getOptUserId(); Integer bizType = getBizType(); try { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod();
// 获取方法注解 ActionRecordLogAspect actionControllerLog = method.getAnnotation(ActionRecordLogAspect.class); if (IGNORE_METHOD.equals(requestMethod) && actionControllerLog == null) { return result; } RecordLogEntity recordLogEntity = new RecordLogEntity(); recordLogEntity.setUserId(optUserId); recordLogEntity.setBizType(bizType); recordLogEntity.setClassName(method.getDeclaringClass().getCanonicalName()); recordLogEntity.setIp(remoteIP); recordLogEntity.setTraceId(AppContext.getContext().getTraceId()); recordLogEntity.setMethodName(method.getName()); recordLogEntity.setLogType(RecordLogTypeEnum.GENERAL.getType()); recordLogEntity.setUrl(url); recordLogEntity.setDescriptions(apiOperation.value()); recordLogEntity.setResultJson(JSON.toJSONString(result)); if (joinPoint.getArgs().length > 0) { Object argOne = joinPoint.getArgs()[0]; if (argOne instanceof Long) { Long refId = (Long) argOne; recordLogEntity.setRefId(refId); } } List<Object> params = new ArrayList<>(); for (Object o : joinPoint.getArgs()) { boolean isFileList = o instanceof List && ((List) o).size() > 0 && ((List) o).get(0) instanceof MultipartFile; boolean isParam = (o instanceof Serializable || o instanceof BeanMap) && !(o instanceof MultipartFile); //避免文件入参 if (isFileList) { params.add(null); } else if (isParam) { params.add(o); } else { params.add(null); } } if (actionControllerLog != null) { String tokens = actionControllerLog.ignoreParams(); String type = actionControllerLog.logType(); recordLogEntity.setLogType(type); recordLogEntity.setDescriptions(actionControllerLog.descriptions()); if (StringUtils.isNotEmpty(tokens)) { String[] tokenArr = tokens.split(";"); for (String token : tokenArr) { if (token.contains(".")) { String sIndex = token.substring(0, token.indexOf(".")); token = token.substring(token.indexOf(".") + 1); int index = Integer.parseInt(sIndex); params.set(index, remove(params.get(index), token)); } else { params.set(Integer.parseInt(token), null); } } } } recordLogEntity.setParam(JSON.toJSONString(params)); logger.info("操作记录,userId[{}],行为[{}]", recordLogEntity.getUserId(), recordLogEntity.getDescriptions()); recordDubboService.create(recordLogEntity); //执行 } catch (Exception e) { logger.error("操作日志记录错误", e); } return result; } private Object remove(Object obj, String token) { try { Map map = JSON.parseObject(JSON.toJSONString(obj), Map.class); if (token.contains(".")) { String sIndex = token.substring(0, token.indexOf(".")); token = token.substring(token.indexOf(".") + 1); map.put(sIndex, remove(map.get(sIndex), token)); } else { map.put(token, null); } return map; } catch (Exception e) { logger.error("参数移除,异常信息:{}", e.getMessage()); return null; } } public Long getOptUserId() { return AppContext.getUserId(); } public Integer getBizType() { return AppContext.getBizType(); } }
3、使用注解
@ActionRecordLogAspect(descriptions = "删除", logType = RecordLogTypeConstant.DELETE)
public ApiResult<Boolean> delete(@RequestBody @Validated Request request) {
/***/
}
4、注解数组入参示例
@DubboReference(timeout = 10000, parameters= {"aaa", "bbb"})
posted on 2021-06-10 14:20 Iversonstear 阅读(55) 评论(0) 编辑 收藏 举报