关闭

spring支持跨域

标签: springjava
417人阅读 评论(0) 收藏 举报
分类:
为了让所有请求都支持跨域,我是这样做的:
web.xml中:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<init-param>
<param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-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);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16942次
    • 积分:361
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:13篇
    • 译文:0篇
    • 评论:0条