最后一个方法:核心的日志记录方法
package com.migu.cm.aspect;
import com.alibaba.fastjson.JSON;
import com.migu.cm.domain.UserOperationLog;
import com.migu.cm.service.UserOperationLogService;
import com.migu.cm.utils.Slf4jLogUtil;
import com.migu.cm.utils.ThreadLocalUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Aspect
@Order(1)
@Component
public class WebLogAspect {
/**
* 站位符:requestUrl>methodType>DeclaringTypeName.SignatureName>requestParams
*/
private final static String BEFORE_MATCH_STANCE_LOG = "{} {} {} {}";
/**
* 站位符:requestUrl>methodType>DeclaringTypeName.SignatureName>requestParams>responseInfo>cost
*/
private final static String AFTER_MATCH_STANCE_LOG = "{} {} {} {} {} {}";
@Autowired
private UserOperationLogService userOperationLogService;
@Pointcut("execution(public * com.migu.cm.web..*.*(..))")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ThreadLocalUtil.set("startTime", System.currentTimeMillis());
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
ThreadLocalUtil.set("loggerTag", request.getHeader("loggerTag") == null ? UUID.randomUUID().toString() : request.getHeader("loggerTag"));
StringBuffer requestUrl = new StringBuffer(request.getRequestURL());
String requestParam = getRequestParam(joinPoint, request);
recordHandlers();
if (!StringUtils.isEmpty(request.getQueryString())) {
requestUrl.append("?").append(URLDecoder.decode(request.getQueryString(), "utf-8"));
}
Slf4jLogUtil.SimpleLogUtil.infoToController(BEFORE_MATCH_STANCE_LOG, requestUrl.toString()
, request.getMethod(), joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()
, requestParam);
// 用户操作记录(对数据增,删,改)
logInterception(request);
}
/**
* 记录每次请求的操作者记录
*/
private void recordHandlers() {
//打印请求用户名
try {
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (userDetails != null) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("username:").append(userDetails.getUsername());
Slf4jLogUtil.SimpleLogUtil.info(strBuilder.toString());
}
}
} catch (Exception e) {
Slf4jLogUtil.SimpleLogUtil.error("doBefore error", e);
}
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {
// 处理完请求,返回内容
Object[] object = joinPoint.getArgs();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
StringBuffer requestUrl = new StringBuffer(request.getRequestURL());
if (!StringUtils.isEmpty(request.getQueryString())) {
requestUrl.append("?").append(URLDecoder.decode(request.getQueryString(), "utf-8"));
}
String requestParam = getRequestParam(joinPoint, request);
String responseParam = getResponseParam(ret);
Slf4jLogUtil.SimpleLogUtil.infoToController(AFTER_MATCH_STANCE_LOG, requestUrl.toString()
, request.getMethod(), joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()
, requestParam, responseParam
, (System.currentTimeMillis() - (long) ThreadLocalUtil.get("startTime")));
ThreadLocalUtil.remove();
}
/**
* 根据请求方式获取requestParam
*
* @param joinPoint
* @param request
* @return
*/
private String getRequestParam(JoinPoint joinPoint, HttpServletRequest request) {
String params = "[]";
if (request.getContentType() != null) {
switch (request.getContentType()) {
case "application/x-www-form-urlencoded":
params = StringUtils.isEmpty(request.getParameterMap()) ? "[]" : JSON.toJSONString(request.getParameterMap());
break;
case "application/json":
params = JSON.toJSONString(joinPoint.getArgs());
break;
case "application/x-www-form-urlencoded; charset=UTF-8":
params = StringUtils.isEmpty(request.getParameterMap()) ? "[]" : JSON.toJSONString(request.getParameterMap());
break;
default:
params = "[]";
break;
}
}
return params;
}
/**
* 根据实际业务处理输出的responseParam
*
* @param ret
* @return
*/
private String getResponseParam(Object ret) {
if (ret instanceof String) {
Map returnUrl = new HashMap(16);
returnUrl.put("returnAddress", ret);
return JSON.toJSONString(returnUrl);
}
return JSON.toJSONString(ret);
}
private final String UNKNOWN = "unknown";
/***
* 用户操作记录,(对数据库增,删,改)
* @param request
*/
public void logInterception(HttpServletRequest request ) {
try {
// TODO 这里处理下, 如果没有2个 / / 就不要拦截了, 造成的index 越界不好
String requestURI = request.getRequestURI();
String path = requestURI.substring(1);
int index = path.indexOf("/");
String sub2 = path.substring(index+1);
int index2 = sub2.indexOf("/");
String sub3 = sub2.substring(0, index2);
if (sub3.indexOf("add") >= 0 || sub3.indexOf("update") >= 0 || sub3.indexOf("cancel") >= 0 || sub3.indexOf("del") >= 0 || sub3.indexOf("toTop") >= 0
|| sub3.indexOf("make") >= 0 || sub3.indexOf("effect") >= 0 || sub3.indexOf("save") >= 0 || sub3.indexOf("insert") >= 0
|| sub3.indexOf("login") >= 0 || sub3.indexOf("edit") >= 0 || sub3.indexOf("approve") >= 0 || sub3.indexOf("modify") >= 0
|| sub3.indexOf("active") >= 0 || sub3.indexOf("doPackage") >= 0 || sub3.indexOf("doDownload") >= 0 || sub3.indexOf("close") >= 0
|| sub3.indexOf("remove") >= 0) {
UserOperationLog userOperationLog = new UserOperationLog();
//日志记录
try {
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (userDetails != null) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("username:").append(userDetails.getUsername());
userOperationLog.setUsername(userDetails.getUsername());
Slf4jLogUtil.SimpleLogUtil.info(strBuilder.toString());
}
}
} catch (Exception e) {
Slf4jLogUtil.SimpleLogUtil.error("doBefore error", e);
}
userOperationLog.setPath(requestURI);
//时间
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
userOperationLog.setIp(ip);
userOperationLog.setCreatetime(new Date());
int paramsIndex = request.getQueryString().lastIndexOf("=");
String params = request.getQueryString().substring(0,paramsIndex-2);
userOperationLog.setParams(params);
userOperationLogService.saveInfo(userOperationLog);
}
} catch (Exception e) {
Slf4jLogUtil.SimpleLogUtil.error("用户操作记录获取异常:", e);
}
}
}