多部分解析器未处理多部分请求

Multipart Request not processed by the Multipart Resolver

我最近通过添加 csrf 支持对我的 spring 应用程序进行了一些更改。为此,我还必须更改以前处理多部分请求的方式。

为了确保多部分请求正确通过,我在 web.xml

中的 org.springframework.web.filter.DelegatingFilterProxy 之前放置了 org.springframework.web.multipart.support.MultipartFilter

虽然它对大多数请求都工作正常,但有些请求在控制器级别没有收到任何请求参数。我调试了一下,发现在这段代码

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");
        }
    }

MultipartFilter 中没有将我的请求处理为多部分。当进行 multipartResolver.isMultipart(processedRequest) 检查时,请求转到 else 部分。

JSP 中的表单有 enctype="multipart/form-data" 个参数。

<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="rightbox">
                    <div id='url'></div>
                </div>
            </div>
            <div class="main-subdiv-urls-msa">
                <div class="rightbox">
                    <div id='req'></div>
                </div>
            </div>
            <div class="main-subdiv-urls-msa">
                <div class="rightbox">
                    <div id='res'></div>
                </div>
            </div>
            <div class="main-subdiv-urls-msa">
                <div class="rightbox">
                    <div id='image'></div>
                </div>
            </div>
            <div class="main-subdiv-urls-msa">
                <div class="rightbox">
                    <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验证方法

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();
    }
}

}

过滤器映射 web.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>

这里唯一特别的是 <input type="file"> 元素是使用 javascript.

动态添加的

我在这里遗漏了什么吗...最后一件事要注意 - 在添加 csrf 支持之前,代码 运行 很好并且在控制器端接收到请求参数

感谢 M.Deinum 询问如何提交请求。

好吧,我通过更改提交表单的方式设法实现了这一目标。

找零钱 javascript 验证函数

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");
       return false;
    }
}

if (isValid) {
    if (checkValueisEqual(webSiteArray, imageNameArray)) {
      return true;
    }
    return false;
} }

表格变化

<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" />