diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/CorsFilter.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/CorsFilter.java new file mode 100644 index 0000000..563ab17 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/CorsFilter.java @@ -0,0 +1,26 @@ +package com.example.EnjoyTripBackend.filter; + +import jakarta.servlet.*; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +public class CorsFilter implements Filter { + + public void init(FilterConfig filterConfig) throws ServletException {} + + public void destroy() {} + + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { + HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; + + httpResponse.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500"); + httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); + httpResponse.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS, GET, DELETE, PUT"); + httpResponse.setHeader("Access-Control-Max-Age", "3600"); + httpResponse.setHeader("Access-Control-Allow-Headers", "Authorization, x-requested-with, origin, content-type, accept"); + chain.doFilter(servletRequest, servletResponse); + } +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/WebConfig.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/WebConfig.java index 0e1a71f..cc1f982 100644 --- a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/WebConfig.java +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/WebConfig.java @@ -3,6 +3,7 @@ import com.example.EnjoyTripBackend.util.SessionUserArgumentResolver; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.Filter; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -11,6 +12,7 @@ import java.util.List; +@Slf4j @Configuration public class WebConfig implements WebMvcConfigurer { @@ -27,4 +29,13 @@ public FilterRegistrationBean LoginCheckFilter() { filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } + + @Bean + public FilterRegistrationBean CorsFilter() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(); + filterRegistrationBean.setFilter(new CorsFilter()); + filterRegistrationBean.setOrder(0); + filterRegistrationBean.addUrlPatterns("/*"); + return filterRegistrationBean; + } } \ No newline at end of file