Print request parameters

The use of Spring AOP unification process print request log

1, the cut point is defined:

@Pointcut(value = "execution(public * com.longc..*.controller..*.*(..))")

Cut-off point configuration instructions refer to: https://www.cnblogs.com/zhangxufeng/p/9160869.html  write more full

2, the processing logic:

Example:

@Component
@Aspect
@Order(-1)
@Slf4j
public class LogParamAspect {
    
    @Pointcut(value = "execution(public * com.longc..*.controller..*.*(..))")
    public void webLog() {
    }

    @Before(value = "webLog()")
    public void doBefore(JoinPoint joinPoint) {
        try {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            String url = request.getRequestURL().toString();
            String classMethod = joinPoint.getSignature().getDeclaringType().getSimpleName() + "." + joinPoint.getSignature().getName();
            Signature signature = joinPoint.getSignature();
            StringBuilder sb = new StringBuilder();
            sb.append("URL -> ").append(url).append(" | ").append("CLASS_METHOD -> ").append(classMethod);
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            if (method.getAnnotation(NotLogHeader.class) == null) {
                String headers = buildRequestHeaders(request);
                if (!StringUtil.isTrimBlank(headers)) {
                    sb.append ( "|") .append ( "the HEADER ->") .append (headers); 
                } 
            }
            if (method.getAnnotation(NotLogParam.class) == null) {
                String buildRequestParams the params = (Request); 
                IF {(StringUtil.isTrimBlank (the params)!) 
                    sb.append ( "|" ) .append ( "PARAMS ->") .append (the params); 
                } 
            } 
            log.info (sb.toString ()); 
        } the catch (the Throwable EX) { 
            log.error ( "HaLogParamAspect error.", EX); 
        } 
    } 

    / ** 
     * request header assembly (need to print only print configuration) 
     * 
     * @see com.longc.core.constant.CommonRequestHeader NEED_LOG_HEADER_LIST 
     * /
    private String buildRequestHeaders(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames == null) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            while (headerNames.hasMoreElements()) {
                String nextElement = headerNames.nextElement();
                if (!NEED_LOG_HEADER_LIST.contains(nextElement)) {
                    continue;
                }
                if (sb.length() > 0) {
                    sb.append("&");
                }
                sb.append(nextElement).append("=").append(request.getHeader(nextElement));
            }
            return sb.toString();
        }
    }

    /**
     * 组装请求参数信息
     */
    private String buildRequestParams(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        if (MapUtil.isBlank(parameterMap)) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (String key : parameterMap.keySet()) {
                if (sb.length() > 0) {
                    sb.append("&");
                }
                sb.append(key).append("=").append(ArrayUtil.toString(parameterMap.get(key)));
            }
            return sb.toString();
        }
    }

}

  

Guess you like

Origin www.cnblogs.com/longc-pub/p/11103030.html