7

I can define a MultipartResolver like this with a maxUploadSize of 10K (10000 bytes):

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000"/>
</bean

However, if the admin needs to upload some large files via the admin interface which exceed this limit, the app needs to be temporarily re-configured to allow this - and then re-configured again to make sure regular users don't exceed this limit.

While this is happening, of course, a regular user could potentially sneak a large file in without receiving a warning.

Is there a way to configure the resolver to use a different maxUploadSize in these two situations?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Tammen
  • 71
  • 1
  • 2

3 Answers3

4

The simplest method is to use differently-configured implementations of the bean for admins instead of for normal users. The most elegant way of doing that is to have a Spring 3.0 @Configuration bean that produces a session-scoped bean instance (I add a scoped proxy below as well, in case you're not using it in a session-scoped bean; otherwise you could just use a simpler annotation like this: @Scope(WebApplicationContext.SCOPE_SESSION)).

@Configuration
public class MultipartResolverBuilder {
    @Bean @Scope(value = WebApplicationContext.SCOPE_SESSION,
           proxyMode = ScopedProxyMode.TARGET_CLASS)
    public CommonsMultipartResolver getMultipartResolver() {
        CommonsMultipartResolver mr = new CommonsMultipartResolver();
        if (user_is_not_admin) {
            mr.setMaxUploadSize(10000);
        }
        return mr;
    }
}

You'll need to add the code to determine whether the user is an admin or not, of course, and you'll need to add in support for scanning for annotation-based configuration (if you've not already got it; <context:annotation-config/>/<context:component-scan .../> are pretty common things to have).

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Worth noting on this answer: If I call my bean method `getMultipartResolver`, Spring complains. I have to call mine `multipartResolver`. Just in case anyone else gets this problem. – Robin Jonsson Mar 22 '14 at 13:52
0

You might need to create two multipartResolver beans, one for normal users and one for admins. In your app you can pick which bean to use based on the user's role.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
  • 1
    `In your app you can pick which bean to use based on the user's role.` -> How do you do that since the resolver is defined to respond to all multipart requests received by the app? – Tammen Jul 07 '11 at 10:53
  • @Tammen, maybe you can see this spring forum's thread for the answer: :http://forum.spring.io/forum/spring-projects/web/317-how-to-handle-max-upload-size-exceeded?p=151546#post151546. You can just define two beans of MultiparResolver and then use the Spring's DI to insert the appropriate bean.Just like the above post do. – andy Aug 23 '14 at 05:31
0

How about resolve this problem with business logic? Just set the maxUploadSize value enough to the admin, and check if the user is admin or not and the file's size.

Whiteship
  • 1,799
  • 3
  • 16
  • 15