Java – how to configure multipartresolver with different maxuploadsize for ordinary users and administrators?
I can use a 10k (10000 byte) maxuploadsize to define such a multipartresolver:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10000"/> </bean
However, if the administrator needs to upload some large files through the management interface that exceeds this limit, the application needs to be temporarily reconfigured to allow this operation – and then reconfigured again to ensure that regular users do not exceed this limit
Of course, when this happens, ordinary users may sneak into large files without warning
Is there any way to configure the parser to use a different maxuploadsize in both cases?
Solution
The simplest way is to use different configured bean implementations for administrators rather than ordinary users The most elegant method is to use spring 3.0 @ configuration bean to generate session scoped bean instances (I'll add a scope proxy below in case you don't use it in session scoped beans; otherwise, you can use a simpler annotation, as follows: @ 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; } }
Of course, you need to add code to determine whether the user is an administrator, and you need to add an annotation based configuration that supports scanning (if you haven't obtained it yet; < context: annotation config / > / < context: component scan... / > is a very common thing