Created
May 1, 2013 12:02
-
-
Save imamabudaud/5494920 to your computer and use it in GitHub Desktop.
Spring: Allow Cross-domain JSON
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 java.io.IOException; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.springframework.web.filter.OncePerRequestFilter; | |
public class CorsFilter extends OncePerRequestFilter { | |
@Override | |
protected void doFilterInternal(HttpServletRequest request, | |
HttpServletResponse response, FilterChain filterChain) | |
throws ServletException, IOException { | |
response.addHeader("Access-Control-Allow-Origin", "*"); | |
if (request.getHeader("Access-Control-Request-Method") != null | |
&& "OPTIONS".equals(request.getMethod())) { | |
// CORS "pre-flight" request | |
response.addHeader("Access-Control-Allow-Methods", | |
"GET, POST, PUT, DELETE"); | |
response.addHeader("Access-Control-Allow-Headers", | |
"X-Requested-With,Origin,Content-Type, Accept"); | |
} | |
filterChain.doFilter(request, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do this filter apply for all the api rest application ?