我们将项目移至 Spring Boot 2,并在发送Content-Type: multipart/form-data的 API 中收到 CORS 错误。它在旧版本上运行良好。
对于 CORS,我们禁用了它,放入以下代码。
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
.exposedHeaders("Content-Disposition");
}
};
}
所有的 API 在旧版本上都可以正常工作,但是现在当我们进入 Spring Boot 2 时,Content-Type: application/json的 api 工作正常。但是在下面的 API 中抛出了 cors 错误。
从源“http://{MyIP}:3000”访问“http://{MyIP}:8080/users/v1/admin/uploadArticleData”的 XMLHttpRequest 已被 CORS 策略阻止:否“访问控制允许” -Origin' 标头存在于请求的资源上。
我的 Zuul 网关出现了一些问题,调用Content-Type: multipart/form-data;时抛出错误。API。错误:-
Caused by: java.lang.IllegalStateException: Cannot convert form data
at org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter$FormBodyRequestWrapper.buildContentData(FormBodyWrapperFilter.java:207) ~[spring-cloud-netflix-zuul-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter$FormBodyRequestWrapper.getContentType(FormBodyWrapperFilter.java:159) ~[spring-cloud-netflix-zuul-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at javax.servlet.ServletRequestWrapper.getContentType(ServletRequestWrapper.java:140) ~[tomcat-embed-core-9.0.21.jar!/:9.0.21]
at org.apache.commons.fileupload.servlet.ServletRequestContext.getContentType(ServletRequestContext.java:74) ~[commons-fileupload-1.3.3.jar!/:1.3.3]
at org.apache.commons.fileupload.FileUploadBase.isMultipartContent(FileUploadBase.java:77) ~[commons-fileupload-1.3.3.jar!/:1.3.3]
at org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:71) ~[commons-fileupload-1.3.3.jar!/:1.3.3]
at org.springframework.web.multipart.commons.CommonsMultipartResolver.isMultipart(CommonsMultipartResolver.java:124) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1164) [spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1011) [spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) [spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) [spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
... 93 common frames omitted
Caused by: org.springframework.http.InvalidMediaTypeException: Invalid mime type "text": does not contain '/'
at org.springframework.http.MediaType.parseMediaType(MediaType.java:534) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.http.MediaType.valueOf(MediaType.java:519) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.cloud.netflix.zuul.util.RequestContentDataExtractor.extractFromMultipartRequest(RequestContentDataExtractor.java:128) ~[spring-cloud-netflix-zuul-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.cloud.netflix.zuul.util.RequestContentDataExtractor.extract(RequestContentDataExtractor.java:64) ~[spring-cloud-netflix-zuul-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter$FormBodyRequestWrapper.buildContentData(FormBodyWrapperFilter.java:193) ~[spring-cloud-netflix-zuul-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
... 103 common frames omitted
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "text": does not contain '/'
at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:197) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.http.MediaType.parseMediaType(MediaType.java:531) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
... 107 common frames omitted