Aop日志拦截

@Aspect
@Component
@Order(1)
public class WriteLogAspect {

@Pointcut("@annotation(com.sunac.hikvision.common.aspect.annotation.WriteLog)")
public void webLog() {
}


@Around("webLog()")
public Object doSurround(ProceedingJoinPoint jp) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ipAddr = getRemoteHost(request);
String uri = request.getRequestURI();
Object[] args = jp.getArgs();

StringBuilder reqParams = new StringBuilder();
for (Object arg : args) {
reqParams.append(arg).append(" ");
}

LogUtil.logInfo(LogEnum.LOG_COMMON.getName(),"请求源IP:【{}】,请求URL:【{}】,请求参数:【{}】",ipAddr,uri,reqParams.toString());
Object result = jp.proceed();
LogUtil.logInfo(LogEnum.LOG_COMMON.getName(),"请求源IP:【{}】,请求URL:【{}】,返回参数:【{}】",ipAddr,uri, JSON.toJSONString(result));

return result;
}

/**
* 获取目标主机的ip
* @param request
* @return
*/
private String getRemoteHost(HttpServletRequest request) {
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();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
}

猜你喜欢

转载自www.cnblogs.com/xjatj/p/12545517.html