切面编程@Aspect 获取 url参数 + requestBody参数
import org.apache.commons.lang3.StringUtils;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.stream.Collectors;
@Aspect
@Slf4j
public class LogAspect {
@Around("@annotation(logRecord)")
public Object validateSignature(ProceedingJoinPoint joinPoint, LogRecord logRecord) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
JSONObject requestParam = StringUtils.isBlank(requestBody) ? JSONUtil.createObj() : JSONUtil.parseObj(requestBody);
requestParam.putAll(request.getParameterMap().entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> String.join(",", entry.getValue()))
)
);
return joinPoint.proceed();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36