在其他Servlet过滤器中解决多部分请求而不会丢失上传内容

发布于 2025-02-09 19:15:35 字数 438 浏览 3 评论 0原文

我的过滤器检查了多个部分的帖子,并可能在到达实际端点之前拒绝它们(球衣,在我的控件之外)。允许休闲多部分解析(如下所示)解决了例外:由于未提供

自定义的CommonSmultipartresolver或现有的分辨率工作,因此无法处理零件,因为在设置属性时,没有错误。但是,访问 /解决之后,内容会丢失。

我可以使用自定义的CommonSmultipartresolver并处理此处建议的丢失信息:在Spring Filter中解析Multipart/Form-DATA请求。但是,我希望在不复制请求的情况下提供一个清洁的解决方案,添加过滤器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2025-02-16 19:15:35

要启用允许casualmultipartparsing tomcat 上下文
您可以注入自定义 tomcatservletwebserverfactory
进入您的应用程序(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);
        }
    };
}

对于Spring Boot 1,工厂类是 tomcatembedservedservletcontainerfactory

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.

故人爱我别走 2025-02-16 19:15:35

查看其余问题(或“我希望有一个更清洁的解决方案添加过滤器,而无需复制请求”),我严重怀疑是否有一个。

该请求仅通过网络发送一次。它包含多个数据,一旦消耗了此信息(即从流中读取)流现在如何通过完整的数据转发到下一个处理器?

我猜这就是通用解决方案只会收到整个数据,决定将其转发到哪里,然后发送整个数据。如果不存储两者之间的数据,它将无法使用,它标记为“复制请求”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文