跨域资源共享(CORS)问题解决方案
CORS:Cross-Origin Resource Sharing(跨域资源共享)
CORS被浏览器支持的版本情况如下:Chrome 3+、IE 8+、Firefox 3.5+、Opera 12+、Safari 4+
问题描述:A域中的脚本请求B域中的资源出现这种问题
报错信息:
XMLHttpRequest cannot load http://localhost:8082/servletdemo/doTest. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access.
问题分析:
两个不同的域之间发送请求,浏览器出于安全因素考虑,所以不允许这种访问。
Cross-Origin Resource Sharing (CORS) is a specification that enables truly open access across domain-boundaries. If you serve public content,
please consider using CORS to open it up for universal JavaScript/browser access.
Granting JavaScript clients basic access to your resources simply requires adding one HTTP Response Header, namely:
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://example.com:8080/
问题解决:
以下提供几种解决方法,根据实际情况选择
一、容器层面,影响范围是容器下的所有webapp应用
in tomcat/conf/web.xml ex:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ps:这个过滤器只针对apache-tomcat-7.0.41及以上版本。
二、单个应用,只作用于这个项目本身
in webapp/WEB-INF/web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、一组资源层面,作用于指定Filter过滤的全部请求资源
Filter方法代码 ex:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.addHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
chain.doFilter(req, res);
}
四、单个资源层面,只针对某一个资源
Servlet方法代码 ex:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
out.write("{/"key/":/"value/"}");
out.flush();
out.close();
}
其中spring framework在4.2及以上支持cors注解,可参考https://spring.io/blog/2015/06/08/cors-support-in-spring-framework;
另一种https://spring.io/guides/gs/rest-service-cors/
五、针对单兵开发,我们原型maven-Archetype-wepapp提供两种支持
tomcat7-maven-plugin
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/servletdemo</path>
<port>8082</port>
<server>tomcat</server>
<url>http://localhost:8080/manager/text</url>
<!-- Enable CORS -->
<tomcatWebXml>src/test/resources/tomcat.web.xml</tomcatWebXml>
</configuration>
</plugin>
jetty-maven-plugin
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.2.v20150730</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/servletdemo</contextPath>
<!--Fix file locking problem with jettyrun Enable CORS-->
<defaultsDescriptor>src/test/resources/jetty.web.xml</defaultsDescriptor>
</webApp>
<httpConnector>
<!-- mvn -Djetty.port=8082 jetty:run -->
<port>8082</port>
</httpConnector>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.3.2.v20150730</version>
</dependency>
</dependencies>
</plugin>
跨域资源共享(CORS)问题解决方案的更多相关文章
- 跨域资源共享CORS与JSONP
同源策略限制: 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果没有同源策略,攻击者可以通过JavaScript获取你的邮件以及其他敏感信息,比如说 ...
- 跨域解决方案 - 跨域资源共享cors
目录 1. cors 介绍 2. 原理 3. cors 解决跨域 4. 自定义HTTP 头部字段解决跨域 5. 代码演示 5. 参考链接 1. cors 介绍 cors 说的是一个机制,其实相当于一个 ...
- VUE SpringCloud 跨域资源共享 CORS 详解
VUE SpringCloud 跨域资源共享 CORS 详解 作者: 张艳涛 日期: 2020年7月28日 本篇文章主要参考:阮一峰的网络日志 » 首页 » 档案 --跨域资源共享 CORS 详解 ...
- 网络编程-跨域资源共享 CORS
目录 1.什么是同源策略? 2.跨域资源共享 CORS 3.预检请求 4.CORS相关字段 5.Golang实现跨域 6.参考资料 1.什么是同源策略? 如果两个 URL 的 protocol.por ...
- 跨域资源共享 CORS
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...
- 跨域资源共享 CORS 详解
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...
- 使Web Api 支持跨域资源共享(CORS)
Reference:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api Imp ...
- 跨域资源共享CORS详解
简介 CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请 ...
- 跨域资源共享 CORS 详解(转)
add by zhj: 公司在一个web产品上,做前后端分离,前后端提供独立的服务,使用不同的域名,通过http进行交互,在 前端,会涉及到跨域访问的问题,前端使用了CORS,后端同时需要做相应修改, ...
随机推荐
- List去重复(不是最简单,但绝对是最易理解)
for (int i = 0; i < courselist.size(); i++) //外循环是循环的次数 { for (int j = courselist.size() - 1 ; j ...
- Oracle笔记二
一.数据库语言分类 二.DML之数据插入 把一个表中的数据查询出来插入另外一个表中. create table student(id number,name varchar2(20),age num ...
- jq点击小图 弹出大图(更新版)
$(function(){ $(".fj1-consult").on("click",function(){ //设置弹框中图片的路径 $(".lay ...
- MAC OSX环境下cordova+Ionic的安装配置
一.简介 1.Ionic是什么 IONIC 是目前最有潜力的一款 HTML5 手机应用开发框架.通过 SASS 构建应用程序,它提供了很多 UI 组件来帮助开发者开发强大的应用. 它使用 JavaSc ...
- css学习笔记 8
标准盒模型:盒子宽 = marginleft+borderleft+paddingleft+width+paddingright+borderright+marginright; 盒子高=margin ...
- Ubuntu中由root用户修改为普通用户的办法
比如你的普通用户名是test 目前是root用户 键入命令 su - test 就可以了
- CSS的一些小技巧
1.黑白图像img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: graysc ...
- 20169212《Linux内核原理与分析》第四周作业
Linux第四周作业 1. 堆栈知识 首先回顾了下堆栈相关的知识,堆栈机制是高级语言可以运行的一个基础,这一块需要重点掌握.函数发生调用时,如图 call指令:将eip的按顺序执行的下一条指令(因为在 ...
- js里实现队列与堆栈
在面向对象的程序设计里,一般都提供了实现队列(queue)和堆栈(stack)的方法,而对于JS来说,我们可以实现数组的相关操作,来实现队列和堆栈的功能,看下面的相关介绍. 一 看一下它们的性质,这种 ...
- Centos 6.5 把自带python 2.6.6, 升级到 2.7
http://blog.csdn.net/jcjc918/article/details/11022345