Method method = ((MethodSignature)point.getSignature()).getMethod();
OperatingAudit audit = ReflectionUtils.getAnnotation(method, OperatingAudit.class);
OperatingRecord record = new OperatingRecord();
record.setStartDate(new Date());
record.setMethod(method.getName());
HttpServletRequest request = SpringMvcHolder.getRequest();
StringBuffer sb = new StringBuffer();
if(request != null) {
record.setIp(getIpAddress(request));
record.setOperatingTarget(request.getRequestURI());
//获取本次提交的参数
Map<String, Object> parameter = request.getParameterMap();
if (parameter.size() > 0) {
sb.append("<h2>request参数</h2>").append("<hr>");
//逐个循环参数和值添加到操作记录参数的描述字段中
for(Entry<String, Object> entry : parameter.entrySet()) {
sb.append("<p>").
append(entry.getKey()).
append(":").
append("<p>").
append("<strong class='text-info'>").
append(getParameterValue(entry.getValue())).
append("</strong>").
append("</p>");
}
sb.append("<hr>");
}
}
//获取当前SessionVariable,通过该变量获取当前用户
SessionVariable sessionVariable = SystemVariableUtils.getSessionVariable();
//如果SessionVariable等于null,表示用户未登录,但一样要记录操作
if (sessionVariable != null) {
record.setFkUserId(sessionVariable.getUser().getId());
record.setUsername(sessionVariable.getUser().getUsername());
}
String function = audit.function();
String module = "";
if (StringUtils.isEmpty(audit.value())) {
OperatingAudit classAnnotation = ReflectionUtils.getAnnotation(method.getDeclaringClass(),OperatingAudit.class);
module = classAnnotation == null ? "" : classAnnotation.value();
}
record.setFunction(function);
record.setModule(module);
Object reaultValue = null;
try {
reaultValue = point.proceed();
record.setState(OperatingState.Success.getValue());
} catch (Throwable e) {
record.setState(OperatingState.Fail.getValue());
StringWriter outputStream = new StringWriter();
PrintWriter printWriter = new PrintWriter(outputStream);
e.printStackTrace(printWriter);
String message = StringUtils.replace(outputStream.toString(), ">", ">");
message = StringUtils.replace(message, "<", "<");
message = StringUtils.replace(message, "\r\n", "<br>");
sb.append("<h2>异常信息</h2>").append("<hr>").append(message).append("<hr>");
request.setAttribute("record", record);
throw e;
} finally {
record.setEndDate(new Date());
record.setRemark(sb.toString());
systemAuditManager.insertOperatingRecord(record);
}
return reaultValue;