Cannot deserialize value of type `int` from String "1/**/and/**/cast(md5('169150...

2026年06月11日 15:52 36.09 秒 success

错误信息

发生未知异常!org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `int` from String "1/**/and/**/cast(md5('1691503582')as/**/int)>0": not a valid `int` value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String "1/**/and/**/cast(md5('1691503582')as/**/int)>0": not a valid `int` value at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 14] (through reference chain: com.md.gjzx.entity.PublicUser["pageIndex"]) at

AI 解决方案

🌐 错误翻译


原文:JSON parse error: Cannot deserialize value of type `int` from String "1/**/and/**/cast(md5('1691503582')as/**/int)>0": not a valid `int` value
译文:JSON解析错误:无法将字符串 "1/**/and/**/cast(md5('1691503582')as/**/int)>0" 反序列化为 `int` 类型:这不是一个有效的 `int` 值

🎯 原因分析


根本原因:前端提交的JSON数据中,pageIndex 字段的值被设置为了一个包含SQL注入攻击特征的字符串,而非后端PublicUser实体类期望的整数(int)类型。

错误发生在Spring的HttpMessageConverter尝试将HTTP请求体(JSON)反序列化为PublicUser对象时。Jackson(JSON解析器)在解析到 pageIndex 字段时,发现其值是一个非数字的字符串("1//and//cast(md5('1691503582')as//int)>0"),因此抛出InvalidFormatException

攻击特征:该字符串是典型的SQL注入Payload,攻击者试图通过修改请求参数来探测数据库漏洞。

💡 解决方案


  • 修复数据类型不匹配:确保PublicUser实体类中pageIndex字段的类型为Integer(推荐使用包装类型,便于处理null)。

  •     public class PublicUser {
            // 使用 Integer 而非 int,可以更好地处理空值
            private Integer pageIndex;
            // 其他属性...
            // getter and setter
        }
        

    • 加强输入校验与安全防护

    • 后端校验:在Controller层使用@Valid注解和@NotNull, @Min等校验注解。
              public ResponseEntity<?> handleRequest(@Valid @RequestBody PublicUser user) {
                  // 业务逻辑
              }
              

              public class PublicUser {
                  @NotNull
                  @Min(1) // 确保 pageIndex 至少为1
                  private Integer pageIndex;
              }
              

      全局异常处理:捕获并友好处理参数校验异常,避免暴露技术细节。
              @RestControllerAdvice
              public class GlobalExceptionHandler {
                  
                  @ExceptionHandler(HttpMessageNotReadableException.class)
                  public ResponseEntity<String> handleJsonParseError(HttpMessageNotReadableException ex) {
                      // 记录详细错误日志(含攻击特征)
                      log.error("JSON解析失败,可能存在恶意参数: {}", ex.getMessage());
                      // 返回通用错误信息给用户
                      return ResponseEntity.badRequest().body("请求参数格式错误");
                  }
                  
                  @ExceptionHandler(MethodArgumentNotValidException.class)
                  public ResponseEntity<String> handleValidationException(MethodArgumentNotValidException ex) {
                      // 提取校验错误信息
                      String message = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
                      return ResponseEntity.badRequest().body(message);
                  }
              }
              

      🛡️ 预防措施


    • 统一API入参校验:为所有接收外部数据的DTO(Data Transfer Object)添加严格的Bean Validation注解(如@NotNull, @Min, @Max, @Pattern),并配合@Valid在Controller中统一校验。

    • 配置安全过滤器:部署并配置Web应用防火墙(WAF) 或使用安全框架(如Spring Security的CSRF防护、输入过滤器)在请求到达业务逻辑层之前拦截明显的恶意请求(如SQL注入、XSS)。

    • 监控与日志:记录所有包含可疑参数的请求日志,并设置告警,以便及时发现和溯源攻击行为。