背景
项目spring依赖版本升级后, 发现post form-data参数无法解析, 所有参数均接收为空, 后经debug复现, 特此记录此问题
原因
原springboot版本2.1.* , 升级到springboot 2.6.13, spring-cloud 2021.0.5
springmvc filter区别在于 HiddenHttpMethodFilter 是否存在
2.1.* 版本默认配置
2.6.* 默认不配置.
经debug, 项目中有自行配置的filter 会读取request.getInputStream, 导致request对象中的useInputStream标记为true, 解析参数时会读取此字段, 为true则不会重复解析参数, 导致参数丢失
添加HiddenHttpMethodFilter 生效后, 此filter在自定义filter执行之前(也就是读取inputStream之前), 执行getParameter()方法, 此方法会触发解析参数逻辑, 解析之后, 后续自定义filter执行读取inputStream不在影响参数解析
解析路径
requestFace --> request.parseParameters -->
if (usingInputStream || usingReader) {
success = true;
return;
}
String contentType = getContentType();
if (contentType == null) {
contentType = "";
}
int semicolon = contentType.indexOf(';');
if (semicolon >= 0) {
contentType = contentType.substring(0, semicolon).trim();
} else {
contentType = contentType.trim();
}
if ("multipart/form-data".equals(contentType)) {
parseParts(false);
success = true;
return;
}
若读取过inputStream之后, parseParts 无法执行
解决办法
1:
spring:
mvc:
hiddenmethod:
filter:
enabled: true
使的HiddenHttpMethodFilter 生效
2
在自定义filter读取inputStream之前, 自行调用request.getParameter或者getParameterMap 或者 getParameterNames, 使得参数解析生效