Last active
April 19, 2020 23:15
-
-
Save ksundong/e6e7e34226e4c55213b5fd28bc9f0af8 to your computer and use it in GitHub Desktop.
Cors 설정
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.stereotype.Component; | |
| import javax.servlet.*; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| @Component | |
| public class SimpleCorsFilter implements Filter { | |
| private final Logger log = LoggerFactory.getLogger(SimpleCorsFilter.class); | |
| public SimpleCorsFilter() { | |
| log.info("SimpleCORSFilter init"); | |
| } | |
| @Override | |
| public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | |
| HttpServletRequest request = (HttpServletRequest) req; | |
| HttpServletResponse response = (HttpServletResponse) res; | |
| response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); | |
| response.setHeader("Access-Control-Allow-Credentials", "true"); | |
| response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, PATCH"); | |
| response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me"); | |
| chain.doFilter(req, res); | |
| } | |
| @Override | |
| public void init(FilterConfig filterConfig) { | |
| } | |
| @Override | |
| public void destroy() { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.web.servlet.config.annotation.CorsRegistry; | |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
| @Configuration | |
| public class WebConfig implements WebMvcConfigurer { | |
| @Override | |
| public void addCorsMappings(CorsRegistry registry) { | |
| registry.addMapping("/**") | |
| .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE") | |
| .allowedOrigins("*") | |
| .allowedHeaders("*") | |
| .allowCredentials(true); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Interceptor | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.web.servlet.HandlerInterceptor; | |
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
| @Configuration | |
| public class WebMvcConfig implements WebMvcConfigurer { | |
| private final HandlerInterceptor interceptor; | |
| public WebMvcConfig(HandlerInterceptor interceptor) {this.interceptor = interceptor;} | |
| @Override | |
| public void addInterceptors(InterceptorRegistry registry) { | |
| registry.addInterceptor(interceptor) | |
| .addPathPatterns("/**") | |
| .excludePathPatterns( | |
| "/login", | |
| "/v2/api-docs", | |
| "/swagger-resources/**", | |
| "/swagger-ui.html", | |
| "/webjars/**"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment