Java – multipart resolver does not process multipart requests
I recently made some changes to my spring application by adding CSRF support To do this, I must also change the way I used to handle multipart requests
To ensure that many requests are passed correctly, I will go to www.org.org springframework. web. multipart. support. Multipartfilter is placed on the web Org. XML springframework. web. filter. Before delegatingfilterproxy
Although it works well for most requests, some requests do not receive any request parameters at the controller level I debugged it and found it in this code
HttpServletRequest processedRequest = request; if (multipartResolver.isMultipart(processedRequest)) { if (logger.isDebugEnabled()) { logger.debug("Resolving multipart request [" + processedRequest.getRequestURI() + "] with MultipartFilter"); } processedRequest = multipartResolver.resolveMultipart(processedRequest); } else { // A regular request... if (logger.isDebugEnabled()) { logger.debug("Request [" + processedRequest.getRequestURI() + "] is not a multipart request"); } }
My request is not processed as a multipart in multipartfilter Multipartresolver When ismultipart (processedrequest) checks, the request goes to the else part
The form that exists in the JSP has an enctype = "multipart / form data" parameter
<form:form modelattribute="configVO" name="ConfigForm" method="post" enctype="multipart/form-data" action="${contextPath}/project/URLconfig"> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> <div class="urldivSales"> <div class="main-subdiv-urls-msa"> <div class="leftlable"> <span>Website Name</span> </div> </div> <div class="main-subdiv-urls-msa"> <div class="leftlable"> <span>Request URL</span> </div> </div> <div class="main-subdiv-urls-msa"> <div class="leftlable"> <span>Response URL</span> </div> </div> <div class="main-subdiv-urls-msa"> <div class="leftlable"> <span>Image Name</span> </div> </div> <div class="main-subdiv-urls-msa"> <div class="leftlable"> <span>Image File</span> </div> </div> <div class="main-subdiv-urls-msa"> <div class="right@R_378_2419@"> <div id='url'></div> </div> </div> <div class="main-subdiv-urls-msa"> <div class="right@R_378_2419@"> <div id='req'></div> </div> </div> <div class="main-subdiv-urls-msa"> <div class="right@R_378_2419@"> <div id='res'></div> </div> </div> <div class="main-subdiv-urls-msa"> <div class="right@R_378_2419@"> <div id='image'></div> </div> </div> <div class="main-subdiv-urls-msa"> <div class="right@R_378_2419@"> <div id='imageFile'></div> </div> </div> <div class="form3buttons"> <input type="button" name="button" id="save" value="Save" onclick="validateForm();" /> <input type="button" name="cancel" id="cancel" value="Cancel" /> </div> </div> </form:form>
JavaScript validation method
function validateForm() { $('#save').attr('disabled','disabled'); var isValid = false; var noOfRows = '${num}'; var webSiteArray = new Array(); var imageNameArray = new Array(); for(var i=0; i<noOfRows; i++) { var web = "web"+i; var req = "req"+i; var res = "res"+i; var image = "image"+i; var webSiteUrl = document.getElementById(web).value; var imageNameValue = document.getElementById(image).value; webSiteArray[i]= webSiteUrl; imageNameArray[i]= imageNameValue; var newReqUrl = document.getElementById(req).value; var newResUrl = document.getElementById(res).value; isValid = checkParm(webSiteUrl,newReqUrl,newResUrl); if (!isValid) { $('#save').removeAttr("disabled"); break; } } if (isValid) { if (checkValueisEqual(webSiteArray,imageNameArray)) { var contextPath = '${contextPath}'+'/project/URLconfig'; document.forms[0].action= contextPath; document.forms[0].submit(); } }
}
On the web Filter mapping in XML
<filter> <filter-name>MultipartFilter</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> </filter> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>MultipartFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The only special thing here is < input type = "file" > using JavaScript to add elements on the fly
I missed something here... The last thing to note is - before adding CSRF support, the code runs normally and receives the request parameters on the controller side
Solution
Thank M. deinum for asking how to submit the request
Well, I'll try to do it by changing the way the form is submitted
This is to change the JavaScript function used for validation
function validateForm() { $('#save').attr('disabled','disabled'); var isValid = false; var noOfRows = '${num}'; var webSiteArray = new Array(); var imageNameArray = new Array(); for(var i=0; i<noOfRows; i++) { var web = "web"+i; var req = "req"+i; var res = "res"+i; var image = "image"+i; var webSiteUrl = document.getElementById(web).value; var imageNameValue = document.getElementById(image).value; webSiteArray[i]= webSiteUrl; imageNameArray[i]= imageNameValue; var newReqUrl = document.getElementById(req).value; var newResUrl = document.getElementById(res).value; isValid = checkParm(webSiteUrl,newResUrl); if (!isValid) { $('#save').removeAttr("disabled"); return false; } } if (isValid) { if (checkValueisEqual(webSiteArray,imageNameArray)) { return true; } return false; } }
change of form
<form:form modelattribute="configVO" name="ConfigForm" method="post" enctype="multipart/form-data" action="${contextPath}/project/URLconfig" onsubmit = "return validateForm();"> <input type="submit" name="button" id="save" value="Save" />