Skip to content

Commit

Permalink
feat(EnjoyTripKorea#6) : CORS filter 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ji-hyeon97 committed May 12, 2024
1 parent d5d7c1c commit 9e8f68e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,7 @@

import java.util.List;

@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

Expand All @@ -27,4 +29,13 @@ public FilterRegistrationBean LoginCheckFilter() {
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}

@Bean
public FilterRegistrationBean CorsFilter() {
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new CorsFilter());
filterRegistrationBean.setOrder(0);
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}

0 comments on commit 9e8f68e

Please sign in to comment.