From 34a453949a3aa9481ddea1566163717a339e4f2e Mon Sep 17 00:00:00 2001 From: sohyundoh Date: Fri, 27 Sep 2024 22:45:26 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[feat]=20interceptor=EC=97=90=EC=84=9C=20OP?= =?UTF-8?q?TIONS=20=ED=99=95=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mile/common/interceptor/MoimAuthInterceptor.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java b/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java index a8b66747..7fbe089b 100644 --- a/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java +++ b/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Objects; import static org.springframework.web.servlet.HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; @@ -32,7 +33,7 @@ public class MoimAuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { - if (handler instanceof ResourceHttpRequestHandler) { + if (handler instanceof ResourceHttpRequestHandler || Objects.equals(request.getMethod(), "OPTIONS")) { return true; } HandlerMethod method = (HandlerMethod) handler; From 02a7836fe4cbe94f82e99f272350c1c3a0120d92 Mon Sep 17 00:00:00 2001 From: sohyundoh Date: Fri, 27 Sep 2024 22:51:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[feat]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/mile/common/CorsTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 module-api/src/test/java/com/mile/common/CorsTest.java diff --git a/module-api/src/test/java/com/mile/common/CorsTest.java b/module-api/src/test/java/com/mile/common/CorsTest.java new file mode 100644 index 00000000..fc6d693d --- /dev/null +++ b/module-api/src/test/java/com/mile/common/CorsTest.java @@ -0,0 +1,32 @@ +package com.mile.common; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class CorsTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void testCorsWithMockMvc() throws Exception { + mockMvc.perform(options("/api/moim/MQ==") + .header("Origin", "http://localhost:5173") + .header("Access-Control-Request-Method", "OPTIONS") + .header("Access-Control-Request-Headers", "Content-Type") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()); + } +} \ No newline at end of file