增加了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; } } }