CORS(跨域资源共享) 的配置

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

兼容情况:

各种新版本的ie10,firefox,opera,safari,chrome以及移动版safari和Android浏览器

ie9及一下版本请使用flash方式来兼容

通过OPTIONS请求握手一次的方式实现跨根域发送请求,需要服务端配置

nginx增加类似如下配置:

[html]view plaincopy

1.server {

2. location / {

3. if ($request_method = 'OPTIONS') {

4. add_header 'Access-Control-Allow-Origin' '*';

5. add_header 'Access-Control-Allow-Credentials' 'true';

6. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

7. add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-

Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Ty pe';

8. # add_header 'Access-Control-Max-Age' 1728000;

9. add_header 'Content-Type' 'text/plain charset=UTF-8';

10. add_header 'Content-Length' 0;

11. return 200;

12. }

13.}

如果没有nginx转发,java需要如下代码:

[html]view plaincopy

1.rundata.getResponse().addHeader("Access-Control-Allow-Origin", "*");

2.rundata.getResponse().addHeader("Access-Control-Allow-Methods", "GET, POST,

OPTIONS");

3.rundata.getResponse().addHeader("Access-Control-Allow-Headers", "Origin, No-

Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Con trol, 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目录下。(可在

上查询并下载。)工程项目中web.xml中的配置如下:[html]view plaincopy

1.

2.CORS

3.com.thetransactioncompany.cors.CORSFilter

4.

5.cors.allowOrigin

6.*

7.

8.

9.cors.supportedMethods

10.GET, POST, HEAD, PUT, DELETE

11.

12.

13.cors.supportedHeaders

14.Accept, Origin, X-Requested-With, Content-Type, Last-Mo

dified

15.

16.

17.cors.exposedHeaders

18.Set-Cookie

19.

20.

21.cors.supportsCredentials

22.true

23.

24.

25.

26.CORS

27./*

28.

/cors-filter-installation.html

兼容情况:

各种新版本的ie10,firefox,opera,safari,chrome以及移动版safari和android浏览器

ie9及一下版本请使用flash方式来兼容

通过OPTIONS请求握手一次的方式实现跨根域发送请求,需要服务端配置

nginx增加类似如下配置:

[html]view plaincopy

1.server {

2. location / {

3. if ($request_method = 'OPTIONS') {

4. add_header 'Access-Control-Allow-Origin' '*';

5. add_header 'Access-Control-Allow-Credentials' 'true';

6. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

7. add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-

Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Ty pe';

8. # add_header 'Access-Control-Max-Age' 1728000;

9. add_header 'Content-Type' 'text/plain charset=UTF-8';

10. add_header 'Content-Length' 0;

11. return 200;

12. }

13.}

相关文档
最新文档