CORS(跨域资源共享) 的配置
来源:互联网 发布:二分搜索算法代码 编辑:程序博客网 时间:2024/05/18 15:56
各种新版本的ie10,firefox,opera,safari,chrome以及移动版safari和 android浏览器
ie9及一下版本请使用flash方式来兼容
通过OPTIONS请求握手一次的方式实现跨根域发送请求,需要服务端配置
nginx增加类似如下配置:
- server {
- location / {
- if ($ request_method = ‘OPTIONS’) {
- add_header ‘Access-Control-Allow-Origin’ ‘*’;
- add_header ‘Access-Control-Allow-Credentials’ ‘true’;
- add_header ‘Access-Control-Allow-Methods’ ‘GET, POST, OPTIONS’;
- add_header ‘Access-Control-Allow-Headers’ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type’;
- # add_header ‘Access-Control-Max-Age’ 1728000;
- add_header ‘Content-Type’ ‘text/plain charset= UTF-8′;
- add_header ‘Content-Length’ 0;
- return 200;
- }
- }
如果没有nginx转发, java需要如下代码
- rundata.getResponse().addHeader(“Access-Control-Allow-Origin”, “*”);
- rundata.getResponse().addHeader(“Access-Control-Allow-Methods”, “GET, POST, OPTIONS”);
- rundata.getResponse().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”);
tomcat下CORS( 跨域资源共享) 的配置
CORS介绍
它在维基百科上的定义是: 跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源。而这种访问是被同源策略所禁止的。CORS系统定义了一种浏览器和服务器交互的方式来确定是否允许跨域请求。 它是一个妥协,有更大的灵活性,但比起简单地允许所有这些的要求来说更加安全。
而W3C的官方文档目前还是工作草案,但是正在朝着W3C推荐的方向前进。
简言之,CORS就是为了让AJAX可以实现可控的跨域访问而生的。
Tomcat下的配置
下载cors-filter-1.7.jar,
java
-property-utils-1.9.jar这两个库文件,放到lib目录下。(可在
http://search.maven.org上查询并下载。)工程项目中web.xml中的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>service.print.cloud</display-name>
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET,POST,HEAD,PUT,DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept,Origin,X-Requested-With,Content-Type,Last-Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
兼容情况:
各种新版本的ie10,firefox,opera,safari,chrome以及移动版safari和 android浏览器
0 0
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- tomcat下CORS(跨域资源共享) 的配置
- tomcat下CORS(跨域资源共享) 的配置
- 跨域资源共享(CORS)
- CORS(跨域资源共享)
- Apache中配置支持CORS(跨域资源共享)实例
- tomcat7.0配置CORS(跨域资源共享)
- tomcat7.0配置CORS(跨域资源共享)
- 跨域资源共享(CORS)
- 跨域资源共享CORS
- CORS 跨域资源共享
- 跨域资源共享 CORS
- 跨域资源共享cors
- 【CodeForce】509F Progress Monitoring(树形情景区间DP)
- Best Time to Buy and Sell Stock III (Java)
- shell写的日期函数
- oracle一个创建用户、创建表空间、授权、建表的完整过程
- linux的open函数
- CORS(跨域资源共享) 的配置
- Java String面试题
- 从相册中取图片奔溃问题解决
- Ant自动编译打包&发布 android项目
- HP UNIX 命令
- Qt Quick里的AnimatedSprite的用法
- SQL注入
- 串口程序设计——struct termios结构体
- 【SSH】之Hibernate