我正在使用AJAX调用其余端点。我正在localhost上工作,并已使用filter设置了CORS配置,并且对于GET和POST正常工作。今天,我需要使用PUT,但是我的浏览器写道 跨域请求被阻止。 “相同起源”不允许在https://localhost:8443/documents处加载资源(原因:CORS请求未成功,在标头CORS中未找到“ access-control-allow-methods”。 正如我所说,这很奇怪,因为它适用于POST和GET。
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsConfig implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Alow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Access-Control-Max-Age","3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization, content-type");
if("OPTIONS".equalsIgnoreCase(request.getMethod())){
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}
@Override
public void destroy() {
}
}