| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package me.zhengjie.aspect;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import lombok.extern.slf4j.Slf4j;
- import me.zhengjie.utils.IpAddressUtil;
- import org.apache.commons.lang3.StringUtils;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.CodeSignature;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.Objects;
- @Slf4j
- @Aspect
- @Component
- public class LogRequestParam {
- @Pointcut("execution(* me.zhengjie.application.*.controller..*.*(..))")
- public void pointCut() {
- }
- public static boolean isJSON(String str) {
- boolean result = false;
- if (StringUtils.isNotBlank(str)) {
- str = str.trim();
- if (str.startsWith("{") && str.endsWith("}")) {
- result = true;
- } else if (str.startsWith("[") && str.endsWith("]")) {
- result = true;
- }
- }
- return result;
- }
- @Before(value = "pointCut()")
- public void doBefore(JoinPoint joinPoint) {
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
- .getRequestAttributes();
- if (Objects.isNull(servletRequestAttributes)) {
- return;
- }
- HttpServletRequest request = servletRequestAttributes.getRequest();
- // 得到请求的ip地址
- String ip = IpAddressUtil.getIpAddress(request);
- String uri = request.getRequestURI();
- // 请求方法
- String method = request.getMethod();
- Object error = "";
- try {
- Object[] parameterValues = joinPoint.getArgs();
- int parameterValuesLength = parameterValues.length;
- if (parameterValuesLength == 0) {
- log.debug("[请求参数] - 无");
- return;
- }
- Signature signature = joinPoint.getSignature();
- if (Objects.isNull(signature)) {
- return;
- }
- String[] parameterNames = ((CodeSignature) signature).getParameterNames();
- if (Objects.isNull(parameterNames) || parameterNames.length != parameterValuesLength) {
- return;
- }
- String paramJson = "";
- for (int i = 0; i < parameterNames.length; i++) {
- Object value = parameterValues[i];
- if (value == null || value instanceof HttpServletRequest || value instanceof HttpServletResponse) {
- continue;
- } else if (value instanceof MultipartFile) {
- MultipartFile part = (MultipartFile) value;
- paramJson = part.getOriginalFilename();
- } else if (value instanceof MultipartFile[]) {
- MultipartFile[] part = (MultipartFile[]) value;
- StringBuffer sb = new StringBuffer();
- for (MultipartFile file : part) {
- sb.append(file.getOriginalFilename() + ",");
- }
- paramJson = sb.toString();
- } else if (value instanceof String) {
- paramJson = (String) value;
- if (paramJson.length() > 1000) {
- // 去掉特别长的参数
- paramJson = paramJson.substring(0, 1000);
- }
- } else {
- // 处理参数是对象的值,如果出现异常,说明有文件流
- error = value;
- paramJson = JSON.toJSONString(value);
- // 先进行赋值
- // value = object;
- if (StringUtils.isNotBlank(paramJson) && paramJson.length() > 1000) {
- StringBuilder sb = new StringBuilder();
- // valLength = fieldValue.substring(0, 1000);
- sb.append(paramJson.substring(0, 1000));
- sb.append(paramJson.substring(paramJson.length() - 1000));
- paramJson = sb.toString();
- }
- }
- }
- // 这里判断
- Object[] obj = { ip, method, uri, paramJson };
- log.info("[request] - ip: {} , method: {} , requestUrl: {} , param: {} ", obj);
- } catch (Exception e) {
- // e.printStackTrace();
- // 这里判断
- Object[] obj = { ip, method, uri, error.toString() };
- log.info("[request] - ip: {} , method: {} , requestUrl: {} , param: {}, ", obj);
- }
- }
- @AfterReturning(returning = "result", pointcut = "pointCut() ")
- public void doAfterReturning(Object result) {
- try {
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
- .getRequestAttributes();
- if (Objects.isNull(servletRequestAttributes)) {
- return;
- }
- HttpServletResponse response = servletRequestAttributes.getResponse();
- if (Objects.isNull(response)) {
- return;
- }
- int status = response.getStatus();
- HttpServletRequest request = servletRequestAttributes.getRequest();
- String json = JSON.toJSONString(result, SerializerFeature.WriteMapNullValue);
- // 打印输出的值
- if (StringUtils.isNotBlank(json) && json.length() < 1500) {
- String uri = request.getRequestURI();
- String method = request.getMethod();
- Object obj[] = { status, method, uri, json };
- log.info("[reponse] - status: {} ,method:{}, url:{} ,result:{}", obj);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|