Skip to content

Instantly share code, notes, and snippets.

@ekirastogi
Created October 22, 2015 09:20
Show Gist options
  • Save ekirastogi/6c818cb4bacb41012f26 to your computer and use it in GitHub Desktop.
Save ekirastogi/6c818cb4bacb41012f26 to your computer and use it in GitHub Desktop.
CORS filter to allow cross domain ajax calls
package com.ekiras.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by ekansh on 22/10/15.
*/
@Component
public class CorsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header");
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