Created
March 9, 2017 17:47
-
-
Save de314/6a1814794d6e3c10ad236b631493cef1 to your computer and use it in GitHub Desktop.
Spring Boot Cors Filter
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.boot.SpringApplication; | |
| import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| import org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.ComponentScan; | |
| import org.springframework.context.annotation.Profile; | |
| import org.springframework.web.servlet.config.annotation.CorsRegistry; | |
| import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
| import javax.servlet.*; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| @ComponentScan | |
| @SpringBootApplication | |
| public class Application { | |
| public static void main(String[] args) { | |
| SpringApplication.run(Application.class, args); | |
| } | |
| @Bean | |
| @Profile("local") | |
| public Filter corsFilter() { | |
| return new Filter() { | |
| public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | |
| HttpServletResponse response = (HttpServletResponse) res; | |
| response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000"); | |
| response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH"); | |
| response.setHeader("Access-Control-Max-Age", "3600"); | |
| response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
| response.setHeader("Access-Control-Expose-Headers", "Location"); | |
| chain.doFilter(req, res); | |
| } | |
| public void init(FilterConfig filterConfig) {} | |
| public void destroy() {} | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment