spring支持跨域

考文章:http://www.programgo.com/article/82213348142/

为了让所有请求都支持跨域,我是这样做的:
web.xml中:
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
param-value>
init-param>
<init-param>
<param-name>dispatchOptionsRequestparam-name>
<param-value>trueparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>

<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
自己的代码里:
public class Constants {
public static String ODIN_CROSS_DOMAIN_ORIGIN = "http://xxxxxx.wandoulabs.com";
public static String ODIN_CROSS_DOMAIN_CREDENTIALS = "true";
public static String ODIN_CROSS_DOMAIN_METHODS = "GET,PUT,POST,DELETE,OPTIONS";
public static String ODIN_CROSS_DOMAIN_HEADERS = "Referer,Accept,Origin,User-Agent,content-type";
}
@RequestMapping(value = "/{[a-z]+}", method = RequestMethod.OPTIONS)
public void commonOptions(HttpServletResponse theHttpServletResponse) throws IOException {
theHttpServletResponse.setHeader("Access-Control-Allow-Methods", Constants.ODIN_CROSS_DOMAIN_METHODS);
theHttpServletResponse.setHeader("Access-Control-Allow-Headers", Constants.ODIN_CROSS_DOMAIN_HEADERS);
theHttpServletResponse.setHeader("Access-Control-Allow-Origin", Constants.ODIN_CROSS_DOMAIN_ORIGIN);
theHttpServletResponse.setHeader("Access-Control-Allow-Credentials",Constants.ODIN_CROSS_DOMAIN_CREDENTIALS);
}

你可能感兴趣的:(spring,java,技术--java)