How to configure MultipartResolver for another maxUploadSize for regular user and administrator?

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

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

However, if the administrator needs to download some large files through the administrator’s interface that exceed this limit, you need to temporarily reinstall the application to do this - configured again to make sure that ordinary users do not exceed this limit .

Although this happens, of course, an ordinary user can potentially sneak a large file without warning.

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

+7
source share
3 answers

The easiest way is to use differently configured bean implementations for administrators instead of regular users. The most elegant way to do this is Spring 3.0 @Configuration bean , which creates an instance of a bean with a session (<I will also add a proxy server with a limited scope if you do not use it in a bean session, otherwise you could just use a simpler annotation as follows image: @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 will need to add code to determine if the user is an administrator or not, of course, and you will need to add support for scanning to configure based on annotations (if you have not received it yet; <context:annotation-config/> / <context:component-scan .../> are pretty common things).

+3
source

You may need to create two multipartResolver beans, one for regular users and one for admins. In your application, you can choose which bean to use based on the user role.

0
source

How to solve this problem using business logic? Just set the maxUploadSize value for the administrator and check if the user is an administrator or not, and the file size.

0
source

All Articles