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; 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