Aop日志拦截

Posted xjatj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}

以上是关于Aop日志拦截的主要内容,如果未能解决你的问题,请参考以下文章

Aop日志拦截

spring中的aop实现各个类方法的日志拦截

用户操作拦截并作日志记录--自定义注解+AOP拦截

通过AOP拦截打印日志,出入参数

springboot AOP全局拦截日志记录

Aop实现日志的service层的拦截