티스토리 뷰

CORS 란 간단히 말해 서로 다른 도메인간 통신을 하는것을 승인하거나 차단하는 것.

 

차단이 되어 있을때는 여러가지 방법( eg. jsonp, proxy, 웹서버설정 등)으로 해결할수 있지만 그중 filter를 이용해서 해결하는 방법을 써본다. 

 

 

 

CORSFilter.java

 

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.HttpServletResponse;

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-Headers", "x-requested-with, origin, content-type, accept");
		response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");

		chain.doFilter(req, response);
	}

	public void init(FilterConfig arg0) throws ServletException {}

	public void destroy() {}
}

 

 

web.xml

 

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>oingdaddy.CORSFilter</filter-class>
</filter>

<filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

 

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30