From 518718d1e9bec1b467f48df51e994f1b7b392268 Mon Sep 17 00:00:00 2001 From: TastSong Date: Fri, 16 Feb 2024 21:38:06 +0800 Subject: [PATCH] fix cors --- .../java/com/tastsong/crazycar/config/CORSFilter.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CrazyCarServer/src/main/java/com/tastsong/crazycar/config/CORSFilter.java b/CrazyCarServer/src/main/java/com/tastsong/crazycar/config/CORSFilter.java index 01c2a06b4..237ad1863 100644 --- a/CrazyCarServer/src/main/java/com/tastsong/crazycar/config/CORSFilter.java +++ b/CrazyCarServer/src/main/java/com/tastsong/crazycar/config/CORSFilter.java @@ -42,7 +42,13 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo response.setHeader("Access-Control-Allow-Headers", "content-type, authorization"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Max-Age", "3600"); - filterChain.doFilter(servletRequest, servletResponse); + if ("OPTIONS".equalsIgnoreCase(request.getMethod())) { + // 对于OPTIONS预检请求直接返回204状态码,无需继续向下执行 + response.setStatus(HttpServletResponse.SC_NO_CONTENT); + } else { + // 对于非OPTIONS请求,让请求继续通过filter链条向下执行 + filterChain.doFilter(servletRequest, servletResponse); + } } catch (Exception e) { log.info("missing Token,or interface error:{}", request.getRequestURI()); response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);