Undertow 在上传超过设置值的分段文件时引发运行时异常



我在Spring Guide上运行完整版本的Spring boot Upload文件指南,但我使用Undertow作为嵌入式servlet而不是Tomcat默认值。它奏效了。

当我尝试上传大小大于配置文件中值的文件时

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

它引发了异常。这是预期行为

使用Tomcat嵌入式servlet,可以通过捕获SizeLimitExceededExceptionMultipartException轻松处理

但是使用 Undertow,它会抛出 RuntimeException 事件,我在全局处理程序中捕获它:

@ControllerAdvice
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler({RuntimeException.class, Exception.class, IOException.class})
    ResponseEntity<?> handleControllerException(Exception exc, HttpServletRequest request, Throwable ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}

异常消息:

java.lang.RuntimeException: java.io.IOException: UT000020: Connection terminated as request was larger than 131072
    at io.undertow.servlet.spec.HttpServletRequestImpl.parseFormData(HttpServletRequestImpl.java:779) ~[undertow-servlet-1.4.20.Final.jar:1.4.20.Final]
    at io.undertow.servlet.spec.HttpServletRequestImpl.getParameter(HttpServletRequestImpl.java:653) ~[undertow-servlet-1.4.20.Final.jar:1.4.20.Final]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]

以下是代码:

控制器

@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {
    storageService.store(file);
    redirectAttributes.addFlashAttribute("message",
            "You successfully uploaded " + file.getOriginalFilename() + "!");
    return "redirect:/";
}

绒球.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-uploading-files</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

有人对我有同样的问题吗?当超过上传的最大文件大小时,如何捕获异常?

任何帮助将不胜感激

也许它能够在某些特定情况下提供帮助。

我在暗流和异常处理方面遇到了同样的问题。

的情况有点不同,我也有 spring-security,所以像 RequestTooBigException 这样的异常是在 spring-security 过滤器中引起的,当其中一些人尝试从请求中读取信息时。

作为解决方案,我使用在导致该异常的第一个安全筛选器之前添加的附加筛选器。(对我来说,这是BearerTokenAuthenticationFilter.class

还有一个额外的过滤器来处理这些异常:

 @Component
 @Slf4j
 public class SecurityRequestToBigExceptionHandlerFilter extends OncePerRequestFilter {
   @Override
   public void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
       FilterChain filterChain) throws ServletException, IOException {
     try {
       filterChain.doFilter(request, response);
     } catch (IllegalStateException | RequestTooBigException | MultiPartParserDefinition.FileTooLargeException ex) {
       if (ex instanceof RequestTooBigException
           || ex instanceof MultiPartParserDefinition.FileTooLargeException) {
         writeResponse(response, ex);
         return;
       }
       Throwable causeException = ex.getCause();
       if (nonNull(causeException)
           && (causeException instanceof RequestTooBigException
           || causeException instanceof MultiPartParserDefinition.FileTooLargeException)) {
         writeResponse(response, causeException);
         return;
       }
       throw ex;
     }
   }
   private void writeResponse(HttpServletResponse response, Throwable ex) throws IOException {
     log.debug("Handle RequestToBig Exception with {}", ex.getMessage());
     response.setStatus(HttpStatus.BAD_REQUEST.value());
     response.getWriter().write(ex.getMessage());
  }
 }

接下来我们需要更改的是安全配置。

有我简化的安全配置:

 @EnableWebSecurity
 @RequiredArgsConstructor
 public class SecurityConfig extends WebSecurityConfigurerAdapter {
   @Autowired
   private final SecurityRequestToBigExceptionHandlerFilter requestToBigExceptionHandlerFilter;
   @Override
   public void configure(HttpSecurity http) throws Exception {
     http 
         .addFilterBefore(requestToBigExceptionHandlerFilter, BearerTokenAuthenticationFilter.class)
         .csrf().disable()
         .cors()
         .and()
         .authorizeRequests()
         .anyRequest().authenticated()
         .and()
         .oauth2ResourceServer().jwt();
   }
 }

相关内容

  • 没有找到相关文章

最新更新