Tomcat 8.5与Spring Boot 2.2环境下 10位以上参数返回404且不进入过滤器
AWS专属Tomcat 8.5部署Spring Boot应用的超长参数拦截问题
核心现象
在AWS上的特定独立Tomcat 8.5实例中运行Spring Boot应用时,只要请求的请求体或查询参数包含位数超过10位的Long类型数值(例如示例值4343434567654),请求甚至不会进入自定义过滤器的执行逻辑,直接被前置拦截。
排查过程使用的代码片段
自定义日志过滤器
该过滤器的核心作用是缓存并记录所有请求、响应的原始数据:
@WebFilter("/*") public class LoggingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper((HttpServletRequest) request); ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response); try { chain.doFilter(requestWrapper, responseWrapper); } finally { String requestBody = new String(requestWrapper.getContentAsByteArray()); String responseBody = new String(responseWrapper.getContentAsByteArray()); System.out.println("requestBody :" + requestBody); System.out.println("responseBody :" + responseBody); responseWrapper.copyBodyToResponse(); } } }
业务测试接口控制器
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class SampleController { @PostMapping("/postdata") public Body postDate(@RequestBody Body body) { return body; } }
请求参数接收实体类
public class Body { private Long commentId; public Long getCommentId() { return commentId; } public void setCommentId(Long commentId) { this.commentId = commentId; } }
全局兜底错误处理逻辑
@PostMapping(value = "/*") public ResponseEntity<ErrorRes> getNotFoundHandler() { ErrorRes error = new ErrorRes(); error.setErrorCode(HttpStatus.NOT_FOUND.toString()); error.setErrorDescription(request.getRequestURI()); return new ResponseEntity<>(error, HttpStatus.FORBIDDEN); }
排查过程留存了Postman测试请求的操作截图作为验证参考。
多场景复现验证结果
- 本地环境使用同版本、更高版本Apache Tomcat运行同一应用,均未复现该问题
- 将应用打包为独立可执行Jar包,在同一台AWS服务器上部署运行,问题也不会触发
- 调整接口入参接收方式,将原本的
@RequestBody Body body改为@RequestBody String body直接接收原始请求体,问题依然存在
待解决诉求
定位该AWS专属Tomcat实例的异常配置项,找到允许请求体、查询参数携带Long类型长数值的调整方案。
内容的提问来源于stack exchange,提问作者vishal sundararajan
相关产品推荐
相关产品推荐

