我有一个Spring Boot API和一个Angular前端。当我从弹簧控制器返回重定向时,我看到此错误。 CORSFilter我的意思是我通常不会收到CORS错误。它只是在我返回重定向时发生的。为什么OPTIONS请求失败?请帮忙!!!
预检响应中的Access-Control-Allow-Headers不允许请求标头字段授权
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class CorsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.addHeader("Access-Control-Allow-Origin", "*");
String options = HttpMethod.OPTIONS.toString();
if (options.equalsIgnoreCase(request.getMethod())) {
response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "content-type,access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with");
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void destroy() {}
}
这是控制器:
@RequestMapping(method = RequestMethod.GET, value = "verify")
public ResponseEntity<?> verify() {
HttpHeaders headers = new HttpHeaders();
headers.add("Location", "http://localhost:4200/some-url");
return responseEntityHelper.okResponseEntity(headers);
}