一、概念

1. 如果两个页面的协议、域名和端口是完全相同的,那么它们就是同源的,不同则为跨域

2. ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作。

3. 例子:

http://www.abc.com/index.html 调用 http://www.abc.com/server.do (非跨域)

http://www.abc.com/index.html 调用 http://www.def.com/server.php (主域名不同:abc/def,跨域)

http://abc.abc.com/index.html 调用 http://def.abc.com/server.php (子域名不同:abc/def,跨域)

http://www.abc.com:8080/index.html 调用 http://www.abc.com:8081/server.do (端口不同:8080/8081,跨域)

http://www.abc.com/index.html 调用 https://www.abc.com/server.do (协议不同:http/https,跨域)

请注意:localhost和127.0.0.1虽然都指向本机,但也属于跨域。

浏览器执行javascript脚本时,会检查这个脚本属于哪个页面,如果不是同源页面,就不会被执行。

二、

1. 文档:http://software.dzhuvinov.com/cors-filter.html

2.  cors--->  Cross-Origin Resource Sharing  --->跨域资源共享

3. 添加依赖

<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>java-property-utils</artifactId>
<version>1.10</version>
</dependency>

4. web.xml 添加如下代码:

<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>

5. 添加上面的过滤器,其实就是在返回的response中,添加如下header

        response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

6. jquery请求也可以是AngularJS

$.ajax("url", {
type: "POST",
//A map of fieldName-fieldValue pairs to set on the native XHR object. For example, you can use it to set withCredentials to true for cross-domain requests if needed.
//XHR:XMLHttpRequest (XHR) ,基于XML技术的Http请求。设置本地XHR对象的“名-值”映射。例如,可以在需要时设置“withCredentials”为真而执行跨域名请求。
//withCredentials: https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/withCredentials
   xhrFields: {
withCredentials: true,
useDefaultXhrHeader: false
},
data: {
type: "test"
},
dataType: 'json',
crossDomain: true,
success: function(data, status, xhr) {
console.log(data);
}
});

  

cors,跨域资源共享,Java配置的更多相关文章

  1. CORS跨域资源共享

    CORS(跨域资源共享)跨域问题及解决 当使用ajax跨域请求时,浏览器报错:XmlHttpRequest error: Origin null is not allowed by Access-Co ...

  2. CORS跨域资源共享你该知道的事儿

    "唠嗑之前,一些客套话" CORS跨域资源共享,这个话题大家一定不陌生了,吃久了大转转公众号的深度技术好文,也该吃点儿小米粥溜溜胃里的缝儿了,今天咱们就再好好屡屡CORS跨域资源共 ...

  3. 在ASP.NET Web API中实现CORS(跨域资源共享)

    默认情况下,是不允许网页从不同的域访问服务器资源的,访问遵循"同源"策略的原则. 会遇到如下的报错: XMLHttpRequest cannot load http://local ...

  4. 跨域漏洞丨JSONP和CORS跨域资源共享

    进入正文之前,我们先来解决个小问题,什么是跨域? 跨域:指的是浏览器不能执行其它网站的脚本,它是由浏览器的同源策略造成的,是浏览器的安全限制! 跨域常见的两种方式,分别是JSONP和CORS. 今天i ...

  5. 浅谈跨域问题,CORS跨域资源共享

    1,何为跨域? 在理解跨域问题之前,你先要了解同源策略和URL,简单叙述: 1)同源策略 三同:协议相同,域名相同,端口相同: 目的:保证用户信息安全,防止恶意网站窃取数据.同源策略是必须的,否则co ...

  6. django上课笔记7-jQuery Ajax 和 原生Ajax-伪造的Ajax-三种Ajax上传文件方法-JSONP和CORS跨域资源共享

    一.jQuery Ajax 和 原生Ajax from django.conf.urls import url from django.contrib import admin from app01 ...

  7. tomcat7.0配置CORS(跨域资源共享)

    平时我们做前台页面时可能会遇到浏览器以下提示(浏览器控制台): 已阻止跨源请求:同源策略禁止读取位于 http://xxx.xxx.com 的远程资源.(原因:CORS 头缺少 'Access-Con ...

  8. Nginx CORS 跨域资源共享问题

    ## 背景 新项目上线,前后端分离,遇到了跨域资源共享的问题,导致请求根本无法发送到后端,前端和后端貌似只能有一个来处理跨域问题,我们这边要采用nginx来解决跨域问题. ## Nginx的CORS配 ...

  9. CORS跨域djangosetting.py 配置

    1 什么是 CORS? Cross-Origin Resource Sharing(CORS)跨域资源共享是一份浏览器技术的规范,提供了 Web 服务从不同域传来沙盒脚本的方法,以避开浏览器的同源策略 ...

随机推荐

  1. php异常处理笔记

    <?php header("Content-type:text/html;charset=utf-8"); // try // { // //业务处理 错误时抛出异常. // ...

  2. postgre数据库插入错误:prepared statement “S_1”already exist, 解决办法

    在使用kettle工具(数据迁移软件)在postgre数据库中插入记录时,出现如下错误,解决办法: 在/etc/pgsql/pgbouncer.ini中修改配置,设置 server_reset_que ...

  3. 有谁知道什么工具测试IOS手机上APP的性能软件啊?

    有谁知道什么工具测试IOS手机上APP的性能软件啊?

  4. npm和git代理

    npm 删除代理设置:npm config delete proxynpm config delete https-proxynpm 设置代理:npm config set proxy http:// ...

  5. jsp <span>标签自动换行

    <span>你好43675373543786375425278687375434537diovfndlbnslvsdlbepsfqwo[ewsbnsdbonfdnb</span> ...

  6. 十二 web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies

    模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于start_urls,start_requests()返回的请求会替代start_urls里 ...

  7. docker的应用

    [root@yz6205 ~]# docker imagesINFO[0063] GET /v1.19/images/json REPOSITORY TAG IMAGE ID CREATED VIRT ...

  8. Zip 压缩

    ICSharpCode.SharpZipLib.dll using ICSharpCode.SharpZipLib.Zip; string[] filenames = Directory.GetFil ...

  9. 记录下一次错误报http请求500,

    1.请求控制层没问题,能请求到,如果缺少参数都会返回提示信息,但是请求参数都对了以后,居然报500,非常不解 找了好久,不知道哪里错了,最后经理提示是不是有可能,mapper.xml出错了,最后,我将 ...

  10. 2017.11.18 IAP下载(STM8,PIC,STM32)

    客户要求用IAP下载,mark一下,客户还给了stm32的引导码.仅供参考. 1 PIC单片机的IAP  2 STm32 IAP https://www.cnblogs.com/WeyneChen/p ...