Pre Merge pull request !1274 from 我素熊猫/master-fix

This commit is contained in:
我素熊猫 2025-03-15 05:49:10 +00:00 committed by Gitee
commit c93a5859a3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 18 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
import cn.iocoder.yudao.framework.web.core.filter.CacheRequestBodyWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.method.HandlerMethod;
@ -44,7 +45,11 @@ public class ApiAccessLogInterceptor implements HandlerInterceptor {
// 打印 request 日志
if (!SpringUtils.isProd()) {
Map<String, String> queryString = ServletUtils.getParamMap(request);
String requestBody = ServletUtils.isJsonRequest(request) ? ServletUtils.getBody(request) : null;
String requestBody = null;
if(ServletUtils.isJsonRequest(request)){
requestBody = (String) request.getAttribute(CacheRequestBodyWrapper.CACHE_BODY);
}
if (CollUtil.isEmpty(queryString) && StrUtil.isEmpty(requestBody)) {
log.info("[preHandle][开始请求 URL({}) 无参数]", request.getRequestURI());
} else {

View File

@ -19,6 +19,13 @@ public class CacheRequestBodyFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
// exception 发生在Servlet(eg: TokenAuthenticationFilter, xxxFilter),
// 并且该请求是被springSecurity过滤的请求比如配置了permit-all_urls的请求
// 代码进入到 ApiAccessLogInterceptor 的时候拿到的是原始的request而不是CacheRequestBodyWrapper
// 这时候不能 getBody from request否则会抛异常 request 已经被消费
CacheRequestBodyWrapper cacheRequestBodyWrapper = new CacheRequestBodyWrapper(request);
String body = ServletUtils.getBody(cacheRequestBodyWrapper);
request.setAttribute(CacheRequestBodyWrapper.CACHE_BODY, body);
filterChain.doFilter(new CacheRequestBodyWrapper(request), response);
}

View File

@ -18,6 +18,11 @@ import java.io.InputStreamReader;
*/
public class CacheRequestBodyWrapper extends HttpServletRequestWrapper {
/**
* 缓存在request的body
*/
public static final String CACHE_BODY = "cache_body";
/**
* 缓存的内容
*/