移动端调用服务接口时,需要给每个接口开始调用的入参以及调用接口名称和方法出参增加日志,方便线上排查错误。
import com.company.project.common.exception.GlobalExceptionHandler;
import com.company.project.common.util.IpUtil;
import com.company.project.common.util.LogAspectUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
/**
* Created with IntelliJ IDEA.
* Description:
*
* @author LErry.li
* Date: 2018-06-16
* Time: 16:31
*/
@Aspect
@Component
public class RestControllerAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 环绕通知
* @param joinPoint 连接点
* @return 切入点返回值
* @throws Throwable 异常信息
*/
@Around("@within(org.springframework.web.bind.annotation.RestController) || @annotation(org.springframework.web.bind.annotation.RestController)")
public Object apiLog(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
boolean logFlag = this.needToLog(method);
if (!logFlag) {
return joinPoint.proceed();
}
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String userAgent = request.getHeader("user-agent");
String ip = IpUtil.getRealIp(request);
String methodName = this.getMethodName(joinPoint);
String params = LogAspectUtil.getMethodParams(joinPoint);
MethodSignature msig = (MethodSignature) joinPoint.getSignature();
Method pointMethod = joinPoint.getTarget().getClass().getMethod(msig.getName(), msig.getParameterTypes());
String methodPath = String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(), pointMethod.getName());
logger.info("开始请求方法:[{}] 服务:[{}] 参数:[{}] IP:[{}] userAgent [{}]", methodName,methodPath,params, ip, userAgent);
long start = System.currentTimeMillis();
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
String deleteSensitiveContent = LogAspectUtil.deleteSensitiveContent(result);
logger.info("结束请求方法:[{}] 参数:[{}] 返回结果[{}] 耗时:[{}]毫秒 ",
methodName, params, deleteSensitiveContent, end - start);
return result;
}
private String getMethodName(ProceedingJoinPoint joinPoint) {
String methodName = joinPoint.getSignature().toShortString();
String shortMethodNameSuffix = "(..)";
if (methodName.endsWith(shortMethodNameSuffix)) {
methodName = methodName.substring(0, methodName.length() - shortMethodNameSuffix.length());
}
return methodName;
}
/**
* 判断是否需要记录日志
*/
private boolean needToLog(Method method) {
return method.getAnnotation(GetMapping.class) == null
&& !method.getDeclaringClass().equals(GlobalExceptionHandler.class);
}
}
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; /** * Created with IntelliJ IDEA. * Description: * AOP记录日志的一些共用方法 * @author LErry.li * Date: 2018-06-17 * Time: 15:19 */ public class LogAspectUtil { private LogAspectUtil(){ } /** * 获取需要记录日志方法的参数,敏感参数用*代替 * @param joinPoint 切点 * @return 去除敏感参数后的Json字符串 */ public static String getMethodParams(ProceedingJoinPoint joinPoint){ Object[] arguments = joinPoint.getArgs(); StringBuilder sb = new StringBuilder(); if(arguments ==null || arguments.length <= 0){ return sb.toString(); } for (Object arg : arguments) { //移除敏感内容 String paramStr; if (arg instanceof HttpServletResponse) { paramStr = HttpServletResponse.class.getSimpleName(); } else if (arg instanceof HttpServletRequest) { paramStr = HttpServletRequest.class.getSimpleName(); } else if (arg instanceof MultipartFile) { long size = ((MultipartFile) arg).getSize(); paramStr = MultipartFile.class.getSimpleName() + " size:" + size; } else { paramStr = deleteSensitiveContent(arg); } sb.append(paramStr).append(","); } return sb.deleteCharAt(sb.length() - 1).toString(); } /** * 删除参数中的敏感内容 * @param obj 参数对象 * @return 去除敏感内容后的参数对象 */ public static String deleteSensitiveContent(Object obj) { JSONObject jsonObject = new JSONObject(); if (obj == null || obj instanceof Exception) { return jsonObject.toJSONString(); } String param = JSON.toJSONString(obj); try { jsonObject = JSONObject.parseObject(param); }catch (Exception e) { return String.valueOf(obj); } ListsensitiveFieldList = getSensitiveFieldList(); for (String sensitiveField : sensitiveFieldList) { if (jsonObject.containsKey(sensitiveField)) { jsonObject.put(sensitiveField, "******"); } } return jsonObject.toJSONString(); } /** * 敏感字段列表(当然这里你可以更改为可配置的) */ private static List getSensitiveFieldList() { List sensitiveFieldList = Lists.newArrayList(); sensitiveFieldList.add("pwd"); sensitiveFieldList.add("password"); return sensitiveFieldList; } }
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created with IntelliJ IDEA. * Description: * IP操作工具类 * @author LErry.li * Date: 2018-06-16 * Time: 16:59 */ public class IpUtil { private IpUtil() { } private static final Logger logger = LoggerFactory.getLogger(IpUtil.class); private static final String IP_PATTERN = "^(?:(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\b"; private static final String UNKNOWN = "unknown"; private static final String LOCAL_IP = "127.0.0.1"; /** * 获取请求中的ip地址:过了多级反向代理,获取的ip不是唯一的,二是包含中间代理层ip * @param request * @return 可能有多个,例如:192.168.1.110, 192.168.1.120 */ public static String getIpAddr(HttpServletRequest request) { String ip = LOCAL_IP; if (request != null) { ip = request.getHeader("x-forwarded-for"); if (StringUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (StringUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } return ip; } /** * 获取客户端请求中的真实的ip地址 * 获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。 * 但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址。而且,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个, * 而是一串ip值,例如:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100。其中第一个192.168.1.110才是用户真实的ip * @param request * @return */ public static String getRealIp(HttpServletRequest request) { String ip = LOCAL_IP; if (request == null) { return ip; } ip = request.getHeader("x-forwarded-for"); ip = getIp(request,ip); if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); if (LOCAL_IP.equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) { //根据网卡取本机配置的IP InetAddress inet = null; try { inet = InetAddress.getLocalHost(); ip = inet.getHostAddress(); } catch (UnknownHostException e) { logger.error("getRealIp occurs error, caused by: ", e); } } } String ch = ","; if (ip != null && ip.contains(ch)) { ip = ip.substring(0, ip.indexOf(ch)); } return ip; } /** * 通过各种方式获取IP * @param request * @param ip * @return */ private static String getIp(HttpServletRequest request, String ip){ 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.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } return ip; } /** * 获取服务器IP */ public static String getServiceIp() { EnumerationnetInterfaces = null; String ipsStr = ""; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration ips = ni.getInetAddresses(); Pattern pattern = Pattern.compile(IP_PATTERN); while (ips.hasMoreElements()) { String ip = ips.nextElement().getHostAddress(); Matcher matcher = pattern.matcher(ip); if (matcher.matches() && !LOCAL_IP.equals(ip)) { ipsStr = ip; } } } } catch (Exception e) { logger.error("getServiceIp occurs error, caused by: ", e); } return ipsStr; } }