/** * 根据配置的请求路径记录操作日志 * */
public ResponseVo<String> save(@Validated(SaveRecord.class) @LogRecord("新增单证") MortgageDocumentRequest mortgageDocumentRequest, @CookieValue(value = "umid", required = false) String umid) { mortgageDocumentService.save(mortgageDocumentRequest, umid); return new ResponseVo<>("新增成功"); }
package com.paic.phucp.console.annotations; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) @Documented public @interface LogRecord { String value() default ""; }
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) @Documented public @interface PrimaryId { }
package com.paic.phucp.console.aop; import com.paic.phucp.console.annotations.LogRecord; import com.paic.phucp.console.annotations.PrimaryId; import com.paic.phucp.dao.mapper.mmapper.UcpDocWebLogMapper; import com.paic.phucp.dao.po.UcpDocWebLog; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.ArrayUtils; 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.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Date; import java.util.List; @Slf4j @Aspect @Component public class LogRecordsAop { @Value("${log.records.mappings:/mortgage}") private String logRecordsMappings; @Resource private UcpDocWebLogMapper ucpDocWebLogMapper; @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") public void logRecords() { // this is an aop pointcut, don't need do anything } @AfterReturning("logRecords()") public void after(JoinPoint joinPoint) throws Throwable { RequestMapping controller = joinPoint.getTarget().getClass().getAnnotation(RequestMapping.class); if (null == controller) { return ; } if (controller.value().length == 0) { return ; } List<String> mappings = Arrays.asList(logRecordsMappings.split(",")); if (!mappings.contains(controller.value()[0])) { return ; } MethodSignature signature = (MethodSignature) joinPoint.getSignature(); RequestMapping requestMapping = signature.getMethod().getAnnotation(RequestMapping.class); if (null == requestMapping) { return ; } Object[] args = joinPoint.getArgs(); Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); GetParamValue getParamValue = new GetParamValue(args, parameterAnnotations).invoke(); Object paramValue = getParamValue.getParamValue(); LogRecord logRecord = getParamValue.getLogRecord(); if (paramValue == null) { return ; } Object logPrimary = getPrimaryId(paramValue); if (logPrimary == null) { return ; } Object createdBy = getCreatedBy(args, parameterAnnotations); saveLog(controller, requestMapping, paramValue, logRecord, logPrimary, createdBy); } private class GetParamValue { private Object[] args; private Annotation[][] parameterAnnotations; private Object paramValue; private LogRecord logRecord; GetParamValue(Object[] args, Annotation[]... parameterAnnotations) { this.args = args; this.parameterAnnotations = parameterAnnotations; } Object getParamValue() { return paramValue; } LogRecord getLogRecord() { return logRecord; } public GetParamValue invoke() { paramValue = null; logRecord = null; for (Annotation[] parameterAnnotation : parameterAnnotations) { int paramIndex = ArrayUtils.indexOf(parameterAnnotations, parameterAnnotation); for (Annotation annotation : parameterAnnotation) { if (annotation instanceof LogRecord) { logRecord = (LogRecord) annotation; paramValue = args[paramIndex]; break; } } if (null != paramValue) { break; } } return this; } } private Object getPrimaryId(Object paramValue) throws IllegalAccessException { Field[] fields = paramValue.getClass().getDeclaredFields(); Object logPrimary = null; for (Field field : fields) { if (!field.isAccessible()) { field.setAccessible(true); } PrimaryId annotation = field.getAnnotation(PrimaryId.class); if (null != annotation) { logPrimary = field.get(paramValue); break; } } return logPrimary; } private Object getCreatedBy(Object[] args, Annotation[][] parameterAnnotations) { Object createdBy = null; for (Annotation[] parameterAnnotation : parameterAnnotations) { int paramIndex = ArrayUtils.indexOf(parameterAnnotations, parameterAnnotation); for (Annotation annotation : parameterAnnotation) { if (annotation instanceof CookieValue) { createdBy = args[paramIndex]; break; } } if (null != createdBy) { break; } } return createdBy; } private void saveLog(RequestMapping controller, RequestMapping requestMapping, Object paramValue, LogRecord logRecord, Object logPrimary, Object createdBy) { byte[] bytes = new Base64().encode(paramValue.toString().getBytes()); UcpDocWebLog log = new UcpDocWebLog(); log.setLogPrimary(logPrimary == null ? "" : logPrimary.toString()); log.setCreatedBy(createdBy == null ? "" : createdBy.toString()); log.setUpdatedBy(createdBy == null ? "" : createdBy.toString()); log.setCreatedDate(new Date()); log.setUpdatedDate(new Date()); log.setRemark(logRecord.value()); log.setRequestParams(new String(bytes, Charset.defaultCharset())); log.setWebAddress(controller.value()[0] + requestMapping.value()[0]); ucpDocWebLogMapper.insert(log); } }