spring支持跨域

为了让所有请求都支持跨域,我是这样做的:
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);
}
阅读更多
文章标签: spring java
个人分类: 技术--java
博主设置当前文章不允许评论。

spring boot 支持js跨域请求

2017年09月09日 647B 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭