跨域资源共享(CORS)问题解决
2017-06-27 16:41
561 查看
今天在部署项目的时候遇到一个小问题,把项目扔在A服务器的tomcat上的时候,数据库在B服务器上。用客户端访问A服务器web项目的时候,使用ajax从数据库中读出的数据会提示【需要跨域资源共享(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:
PS:这个过滤器只针对apache-tomcat-7.0.41及以上版本。
二、单个应用,只作用于这个项目本身 in webapp/WEB-INF/web.xml
三、一组资源层面,作用于指定Filter过滤的全部请求资源 Filter方法代码 ex:
四、单个资源层面,只针对某一个资源 Servlet方法代码 ex:
另一种https://spring.io/guides/gs/rest-service-cors/
五、针对单兵开发,我们原型maven-Archetype-wepapp提供两种支持 tomcat7-maven-plugin
jetty-maven-plugin
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>
相关文章推荐
- 解决ajax跨域请求的问题-cors(资源共享方案)
- Java服务端Cors跨域资源共享配置,解决与Spring Security冲突引起的问题
- 跨域资源共享(CORS)问题解决方案
- java服务端解决js跨域的问题 CORS(跨域资源共享) 的配置
- CORS(跨域资源共享)跨域问题及解决
- Java服务端Cors跨域资源共享配置,解决与Spring Security冲突引起的问题
- 解决前后端分离的 CORS 跨域资源共享问题
- 可以将资源移动到相同的域名上或者启用 CORS 来解决这个问题(跨域问题)、options请求报错
- 跨域问题 XHR实现跨域资源共享(CORS)
- 跨域资源共享问题 tomcat7.0配置CORS(跨域资源共享)
- 跨域资源共享CORS以及在zepto中使用遇到的问题
- 浅析jsopn跨域请求原理及cors(跨域资源共享)的完美解决方法
- HTML5安全:CORS(跨域资源共享)简介
- win7问题解决,凭据管理器和无法访问,不允许一个用户使用一个以上用户名与服务器或共享资源进行多重连接。
- 使用HTML5 跨域共享特性解决AJAX跨域数据同步问题
- [CORS:跨域资源共享] 同源策略与JSONP
- 跨域资源共享(CORS)在ASP.NET Web API中是如何实现的?
- 跨域问题及解决(2)——CORS
- win7问题解决,凭据管理器和无法访问,不允许一个用户使用一个以上用户名与服务器或共享资源进行多重连接。
- 跨域资源共享(CORS)在ASP.NET Web API中是如何实现的?