| New file |
| | |
| | | package com.zy.acs.manager.common.config; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.framework.common.R; |
| | | import com.zy.acs.manager.common.utils.IpTools; |
| | | import com.zy.acs.manager.manager.service.IntegrationRecordService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.MethodParameter; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.converter.HttpMessageConverter; |
| | | import org.springframework.http.server.ServerHttpRequest; |
| | | import org.springframework.http.server.ServerHttpResponse; |
| | | import org.springframework.http.server.ServletServerHttpRequest; |
| | | import org.springframework.web.bind.annotation.ControllerAdvice; |
| | | import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.BufferedReader; |
| | | import java.io.InputStreamReader; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | @Slf4j |
| | | //@ControllerAdvice |
| | | public class IntegrationRecordAdvice implements ResponseBodyAdvice<Object> { |
| | | |
| | | @Autowired |
| | | private IntegrationRecordService integrationRecordService; |
| | | |
| | | @Override |
| | | public boolean supports(@NotNull MethodParameter methodParameter, @NotNull Class<? extends HttpMessageConverter<?>> aClass) { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public Object beforeBodyWrite(Object o |
| | | , @NotNull MethodParameter methodParameter |
| | | , @NotNull MediaType mediaType |
| | | , @NotNull Class<? extends HttpMessageConverter<?>> aClass |
| | | , @NotNull ServerHttpRequest serverHttpRequest |
| | | , @NotNull ServerHttpResponse serverHttpResponse) { |
| | | if (serverHttpRequest instanceof ServletServerHttpRequest) { |
| | | HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest(); |
| | | Object appAuth = request.getAttribute("appAuth"); |
| | | if (appAuth != null) { |
| | | if (o instanceof R) { |
| | | String appkey = request.getHeader("appkey"); |
| | | Object reqCache = request.getAttribute("cache"); |
| | | if (!Cools.isEmpty(appkey)) { |
| | | // 保存接口日志 |
| | | // apiLogService.save( |
| | | // String.valueOf(appAuth), |
| | | // request.getRequestURI(), |
| | | // appkey, |
| | | // IpTools.gainRealIp(request), |
| | | // reqCache==null?"": JSON.toJSONString(reqCache), |
| | | // JSON.toJSONString(o), |
| | | // String.valueOf(((R) o).get("code")).equalsIgnoreCase("200") |
| | | // ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return o; |
| | | } |
| | | |
| | | public static String json(HttpServletRequest request) { |
| | | try { |
| | | BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8)); |
| | | StringBuilder sb = new StringBuilder(); |
| | | String inputStr; |
| | | while ((inputStr = streamReader.readLine()) != null) { |
| | | sb.append(inputStr); |
| | | } |
| | | return sb.toString(); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | } |