parent
39a8f28b20
commit
17e3523b83
|
|
@ -0,0 +1,63 @@
|
||||||
|
package com.nflg.product.bomnew.advice;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.nflg.product.bomnew.util.JsonUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import nflg.product.common.constant.STATE;
|
||||||
|
import nflg.product.common.util.JwtUtil;
|
||||||
|
import nflg.product.common.vo.ResultVO;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.HttpInputMessage;
|
||||||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 曹鹏飞
|
||||||
|
* @date 2024/6/18 09:41:31
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@ControllerAdvice
|
||||||
|
@RefreshScope
|
||||||
|
public class LoggingRequestBodyAdvice implements RequestBodyAdvice {
|
||||||
|
|
||||||
|
@Value("${logging.http.detailed:false}")
|
||||||
|
private Boolean enableHttpDetailed;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return enableHttpDetailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return httpInputMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
try {
|
||||||
|
log.info("请求参数: {}", JsonUtil.toJson(o));
|
||||||
|
log.info("请求头: {}", JsonUtil.toJson(httpInputMessage.getHeaders()));
|
||||||
|
String token = httpInputMessage.getHeaders().getFirst("Authorization");
|
||||||
|
if (StrUtil.isNotBlank(token)) {
|
||||||
|
ResultVO result = JwtUtil.parse(token);
|
||||||
|
if (result.getState().equals(STATE.Success.getState())) {
|
||||||
|
log.info("请求用户: {}", JsonUtil.toJson(result.getData()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("读取请求体出错", ex);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.nflg.product.bomnew.advice;
|
||||||
|
|
||||||
|
import com.nflg.product.bomnew.util.JsonUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
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.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 曹鹏飞
|
||||||
|
* @date 2024/6/18 09:42:06
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@ControllerAdvice
|
||||||
|
@RefreshScope
|
||||||
|
public class LoggingResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||||
|
|
||||||
|
@Value("${logging.http.detailed:false}")
|
||||||
|
private Boolean enableHttpDetailed;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return enableHttpDetailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
|
||||||
|
try {
|
||||||
|
log.info("响应数据: {}", JsonUtil.toJson(o));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("读取响应数据出错", ex);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue