| | |
| | | import org.springframework.web.method.HandlerMethod; |
| | | import org.springframework.web.servlet.HandlerInterceptor; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | import org.springframework.web.util.ContentCachingRequestWrapper; |
| | | |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.lang.reflect.Method; |
| | | import java.util.Date; |
| | | import java.util.LinkedHashMap; |
| | |
| | | return; |
| | | } |
| | | OperateLog operateLog = (OperateLog) obj; |
| | | operateLog.setRequest(buildRequestContent(request)); |
| | | operateLog.setResponse(buildResponseContent(response, ex)); |
| | | try { |
| | | operateLogService.save(operateLog); |
| | |
| | | operateLog.setAction(Cools.isEmpty(memo) ? request.getRequestURI() : memo); |
| | | operateLog.setIp(request.getRemoteAddr()); |
| | | operateLog.setUserId(userId); |
| | | operateLog.setRequest(JSON.toJSONString(request.getParameterMap())); |
| | | operateLog.setCreateTime(new Date()); |
| | | request.setAttribute(ATTR_OPERATE_LOG, operateLog); |
| | | } |
| | | |
| | | private String buildRequestContent(HttpServletRequest request) { |
| | | String body = readCachedBody(request); |
| | | if (!Cools.isEmpty(body)) { |
| | | return body; |
| | | } |
| | | return JSON.toJSONString(request.getParameterMap()); |
| | | } |
| | | |
| | | private String readCachedBody(HttpServletRequest request) { |
| | | if (!(request instanceof ContentCachingRequestWrapper)) { |
| | | return null; |
| | | } |
| | | ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request; |
| | | byte[] content = wrapper.getContentAsByteArray(); |
| | | if (content == null || content.length == 0) { |
| | | return null; |
| | | } |
| | | Charset charset = StandardCharsets.UTF_8; |
| | | if (!Cools.isEmpty(wrapper.getCharacterEncoding())) { |
| | | try { |
| | | charset = Charset.forName(wrapper.getCharacterEncoding()); |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | String body = new String(content, charset).trim(); |
| | | return body.isEmpty() ? null : body; |
| | | } |
| | | |
| | | private String buildResponseContent(HttpServletResponse response, Exception ex) { |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("status", response.getStatus()); |