Created
June 4, 2015 03:46
-
-
Save adilkurniaramdan/041d59287b5de979e0c1 to your computer and use it in GitHub Desktop.
CORSFilterConfig.java
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
package com.qinti.vi.config; | |
/** | |
* Created by adilramdan on 29/08/14. | |
*/ | |
import org.springframework.context.annotation.Configuration; | |
import javax.servlet.*; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
@Configuration | |
public class CORSFilterConfig implements Filter { | |
static final String ORIGIN = "Origin"; | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
} | |
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | |
HttpServletResponse response = (HttpServletResponse) res; | |
HttpServletRequest request = (HttpServletRequest) req; | |
String origin = request.getHeader(ORIGIN); | |
response.setHeader("Access-Control-Allow-Origin", origin); | |
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE"); | |
response.setHeader("Access-Control-Max-Age", "3600"); | |
response.setHeader("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, x-auth-token, x-requested-with"); | |
response.setHeader("Access-Control-Allow-Credentials", "true"); | |
if (request.getMethod().equals("OPTIONS")) { | |
try { | |
response.getWriter().print("OK"); | |
response.getWriter().flush(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
chain.doFilter(req, res); | |
} | |
} | |
@Override | |
public void destroy() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment