Home > Blockchain >  Resolving multi-part request in additinal servlet Filter without losing uploaded content
Resolving multi-part request in additinal servlet Filter without losing uploaded content

Time:06-29

My Filter inspects multi-part posts and potentially rejects them before they reach the actual endpoint (jersey, outside my control). Allowing casual multipart parsing (as shown in answer below) solves the exception: Unable to process parts as no multi-part configuration has been provided

A custom CommonsMultipartResolver or the existing resolver work without the error when the property is set. However, the content gets lost after accessing / resolving it.

I could use a custom CommonsMultipartResolver and deal with the lost information as suggested here: Resolving multipart/form-data request in spring filter. However, I am hoping for a cleaner solution adding the filter, without copying the request.

CodePudding user response:

To enable the allowCasualMultipartParsing property of the Tomcat Context, you can inject a custom TomcatServletWebServerFactory into your application (Spring Boot 2):

import org.apache.catalina.Context;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;

@Bean
public TomcatServletWebServerFactory tomcatFactory()
{
    return new TomcatServletWebServerFactory()
    {
        @Override
        protected void postProcessContext(Context context)
        {
            context.setAllowCasualMultipartParsing(true);
        }
    };
}

For Spring Boot 1, the factory class is TomcatEmbeddedServletContainerFactory.

CodePudding user response:

Looking at the remainder of the question (or 'I am hoping for a cleaner solution adding the filter, without copying the request'), I seriously doubt there is one.

The request is sent over the network once only. It contains multipart data, and once this is consumed (i.e. read from the stream) how would the stream now get forwarded to the next processor with the complete data?

That's where I guess generic solutions just receive the whole data, decide where to forward it to and then send the whole data. It will not work without having stored the data inbetween, what is labelled as 'copying the request' by you.

  • Related