1、问题解析
当以整数数据类型(包括字节、短、长和其他类型)存储的值过大(大于变量可容纳的最大值)的值时,将发生整数溢出(或环
绕)。整数的最高有效位丢失,而其余值则相对于最小值(对于带符号类型,为 0 或负值)。简单来说就是实体类是个Integer类型,但是请求的时候传了值超过了Integer的最大取值范围报错了,但是不能返回异常响应(如500、400等),否则就认为没有对这种情况进行处理。
2、修复建议
- 在问题api成批分配(跳转链接)的基础上修改 ,针对InputCoercionException异常单独处理。
@ExceptionHandler(value = HttpMessageNotReadableException.class)
public ResponseEntity<ResponseDTO> methodHttpMessageNotReadableExceptionHandler(
HttpServletRequest request, HttpMessageNotReadableException e) {
// 判断是否是整数溢出问题
if (e.getRootCause() instanceof InputCoercionException || e.getRootCause() instanceof InvalidFormatException) {
logger.error("类型转换错误");
logger.error("请求地址:{}", request.getServletPath());
ResponseDTO responseDTO = WebUtils.createFailureResponse(ErrorCodeEnum.S_REQ_PARAM_ERROR, "非法的请求参数");
return new ResponseEntity<>(responseDTO, HttpStatus.OK);
}
logger.error("请求body中存在实体类中不存在的参数");
logger.error("请求地址:{}", request.getServletPath());
ResponseDTO responseDTO = WebUtils.createFailureResponse(ErrorCodeEnum.S_REQ_PARAM_ERROR, "请求参数不正确");
return new ResponseEntity<>(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
}
3、问题重新参数
请求参数的 ->“page” 本来应该传1 ,但是传了99999999999999999999,
测试传了page为99999999999999999999的请求和响应:
POST /ngbsp-api/user-act/admin/discuss/black/list HTTP/1.1
Host: 10.110.120.101:89
Accept: application/json, text/plain, */*
X-CSRF-TOKEN: a6a5d13a-74e5-4507-bbc9-4fc0ccf94d7a
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36Content-Type: application/json;
Origin: http://10.110.120.101:89
Referer: http://10.110.120.101:89/ngbsp/
Accept-Language: en-US
Connection: keep-alive
Cookie: LCUID=LzWATXrrydZKuIW4n18FFtf1mAoEUwqHy2AO0zGqMk9wz7l3tiOqZXpz/seb3o4IOyVQA4jS93S2k5OpJyRS8W62Y5Rxx+fTURpA+eQzD5g=;NGBSPSID=OGEwMjlmOTAtMDEzZi00NGY2LTk0OGQtOTM3YTg1NjYyYThj; LCSSID=OGEwMjlmOTAtMDEzZi00NGY2LTk0OGQtOTM3YTg1NjYyYThjContent-Length: 58
{
"page": 99999999999999999999,
"pageSize": 10,
"userPhone": ""
}
HTTP/1.1 500 Internal Server Error
Server: nginx/1.21.6
Date: Wed, 24 Jul 2024 09:02:34 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
Referrer-Policy: no-referrer
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: 1 ; mode=block
{
"timestamp": "2024-07-24 17:02:34",
"status": 500,
"error": "Internal Server Error",
"path": "/user-act/admin/discuss/black/list"
}