Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Created April 25, 2019 02:42
Show Gist options
  • Save luanvuhlu/274fd047ebe7d4760614434bdb8ed2de to your computer and use it in GitHub Desktop.
Save luanvuhlu/274fd047ebe7d4760614434bdb8ed2de to your computer and use it in GitHub Desktop.
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
@Component
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Do not need implement
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, x-auth-token, Content-Type");
response.addHeader("Access-Control-Expose-Headers", "x-auth-token");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
public void destroy() {
// Do not need implement
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment