增加了CorsFilter后,有些浏览器会报403错误。原因是这些浏览器在发送某些POST请求时(一些没有参数的POST请求)没有带Content-Type
在 checkRequestType 函数中
else if ("POST".equals(method)) { String mediaType = getMediaType(request.getContentType()); if (mediaType != null) { if (SIMPLE_HTTP_REQUEST_CONTENT_TYPE_VALUES .contains(mediaType)) { requestType = CORSRequestType.SIMPLE; } else { requestType = CORSRequestType.ACTUAL; } } /* 没有Content类型时(mediaType == NULL) 返回的是 CORSRequestType.INVALID_CORS 后面CorsFilter的处理是给予一个403*/ }
解决方式
else if ("POST".equals(method)) { String mediaType = getMediaType(request.getContentType()); if( null == mediaType ){ requestType = CORSRequestType.SIMPLE }else{ if (SIMPLE_HTTP_REQUEST_CONTENT_TYPE_VALUES .contains(mediaType)) { requestType = CORSRequestType.SIMPLE; } else { requestType = CORSRequestType.ACTUAL; } } }