spring boot / cloud (六) 开启CORS跨域访问

前言

什么是CORS?

Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一个不同源的服务器发出XMLHttpRequest请求,从而克服了ajax只能请求同源服务的限制.并且也可以通过灵活的设置,来指定什么样的请求是可以被授权的.

什么是跨域?

假设你在http://xxx.com/test/下有一个js文件,从这个js里发出一个ajax请求请求后端服务,按照如下情况判定:

请求地址 原因 结果
http://xxx.com/xxxx/action 同一域名,不同文件夹 非跨域
http://xxx.com/test/action 同一域名,同一文件夹 非跨域
http://a.xxx.com/test/action 不同域名,文件路径相同 跨域
http://xxx.com:8080/test/action 同一域名,不同端口 跨域
https://xxx.com/test/action 同一域名,不同协议 跨域

还有那些其他的跨域解决方案?

  • JSONP : 动态添加一个<script>标签,而script标签的src属性是没有跨域的限制的。这样说来,这种跨域方式其实与ajax XmlHttpRequest协议无关了,而缺点也很明显,它只支持GET请求而不支持POST等其它类型的HTTP请求;它只支持跨域HTTP请求这种情况,不能解决不同域的两个页面之间如何进行JavaScript调用的问题

  • NGINX代理 : 通过一个代理服务器,将跨域的请求转发,如:前端JS在http://www.demo.com/a.js,后端是http://www.abc.com/app/action,通过代理可将后端的地址转换成http://www.demo/app/action,这样,从前端发起的请求,就不存在跨域的情况了

然后CORS是支持所有类型的HTTP请求,并且也只是服务端进行设置即可,但是缺点就是老的浏览器不支持CORS(如:IE7,7,8,等)

思路

CORS的响应头

  • Access-Control-Allow-Origin : 必须的,允许的域名,如果设置*,则表示接受任何域名

  • Access-Control-Allow-Credentials : 非必须的,表示是否允许发送Cookie,注意,当设置为true的时候,客户端的ajax请求,也需要将withCredentials属性设置为true

  • Access-Control-Expose-Headers : 非必须的,表示客户端能拿到的header,默认情况下XMLHttpRequestgetResponseHeader方法只能拿到几个基本的header,如果有自定义的header要获取的话,则需要设置此值

  • Access-Control-Request-Method : 必须的,表示CORS上会使用到那些HTTP方法

  • Access-Control-Request-Headers : 必须的,表示CORS上会有那些额外的的有信息

CORS将请求分为两种类型

两种类型分别为简单请求非简单请求,同时满足以下两大条件的请求被定义为是简单请求:

  • 请求方法是以下三种之一:

  • HEAD

  • GET

  • POST

  • HTTP头信息不超出以下几种字段:

  • Accept

  • Accept-Language

  • Content-Language

  • Last-Event-ID

  • Content-Type:只限于三个值application/x-www-form-urlencoded、multipart/form-data、text/plain

对于非简单请求,浏览器会自动发一个预检请求,这个请求是OPTIONS方法的,主要是询问服务器当前请求是否在允许范围内

实现

1.方式A:使用@CrossOrigin来标记指定的方法(小范围跨域)

@RequestMapping(value = "add", method = RequestMethod.GET)
@CrossOrigin(methods = { RequestMethod.GET, RequestMethod.POST }, origins = "*")
public RestResponse<Integer> add(Integer a, Integer b) {
return new RestResponse<>(demoService.add(a, b));
}

2.方式B:使用spring boot的默认配置来设定全局跨域

endpoints.cors.allow-credentials=
endpoints.cors.allowed-headers=
endpoints.cors.allowed-methods=GET
endpoints.cors.allowed-origins=
endpoints.cors.exposed-headers=
endpoints.cors.max-age=1800

3.方式C:使用WebMvcConfigurer自定义配置跨域

定义CorsRegistrationConfig类

public static class CorsRegistrationConfig {
//描述 : 扫描地址
private String mapping = "/**";
//描述 : 允许证书
private Boolean allowCredentials = null;
//描述 : 允许的域
private String allowedOrigins = "*";
//描述 : 允许的方法
private String allowedMethods = "POST,GET,DELETE,PUT";
//描述 : 允许的头信息
private String allowedHeaders = "*"; .........省略
}

定义CorsConfig类

@Configuration
@ConfigurationProperties(prefix = "org.itkk.cors")
@Validated
public class CorsConfig { //描述 : 跨域信息
@NotNull
private Map<String, CorsRegistrationConfig> config; .....省略 }

定义重写addCorsMappings方法

  @Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
//扫描地址
if (!CollectionUtils.isEmpty(config)) {
Iterator<String> keys = config.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
CorsRegistrationConfig item = config.get(key);
CorsRegistration cr = registry.addMapping(item.getMapping());
if (item.getAllowCredentials() != null) {
cr.allowCredentials(item.getAllowCredentials());
}
if (StringUtils.isNotBlank(item.getAllowedOrigins())) {
String[] allowedOriginArray = item.getAllowedOrigins().split(",");
cr.allowedOrigins(allowedOriginArray);
}
if (StringUtils.isNotBlank(item.getAllowedMethods())) {
String[] allowedMethodArray = item.getAllowedMethods().split(",");
cr.allowedMethods(allowedMethodArray);
}
if (StringUtils.isNotBlank(item.getAllowedHeaders())) {
String[] allowedHeaderArray = item.getAllowedHeaders().split(",");
cr.allowedHeaders(allowedHeaderArray);
}
}
}
}
};
}

配置文件,可根据不同的mapping设置不同的cors规则

org.itkk.cors.config.demo.mapping=/**
org.itkk.cors.config.demo.allowCredentials=
org.itkk.cors.config.demo.allowedOrigins=
org.itkk.cors.config.demo.allowedMethods=
org.itkk.cors.config.demo.allowedHeaders=

使用jquery,在跨域场景下进行测试

    $(function(){
$.ajax({
url:'http://127.0.0.1:8080/demo/c',
headers:{
'aheader':'111'
},
type:'GET',
dataType:'json',
success:function(data){
console.log(1);
console.log(data);
console.log(2); }
});
});

代码仓库 (博客配套代码)

结束

演示了单spring boot的应用的,在后续的章节中,会找机会写一下在微服务场景下(spring cloud)的跨域设置


想获得最快更新,请关注公众号

spring boot / cloud (六) 开启CORS跨域访问的更多相关文章

  1. Spring Boot 2中对于CORS跨域访问的快速支持

    原文:https://www.jianshu.com/p/840b4f83c3b5 目前的程序开发,大部分都采用前后台分离.这样一来,就都会碰到跨域资源共享CORS的问题.Spring Boot 2 ...

  2. Spring Boot Web应用开发 CORS 跨域请求支持:

    Spring Boot Web应用开发 CORS 跨域请求支持: 一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等CORS与JSONP相比 1. JSONP只能实现 ...

  3. Spring Boot Web应用开发 CORS 跨域请求支持

    一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等 CORS与JSONP相比 1. JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求. 2. 使用C ...

  4. 九、Spring Boot 优雅的实现CORS跨域

    前言 我们的springboot 架手架已经包含了mysql,redis,定时任务,邮件服务,短信服务,文件上传下载,以及docker-compose 构建镜像等等. 接下来让我们解决另一个常见的问题 ...

  5. nodejs(15)express开启cors跨域

    express开启cors跨域 package.json "dependencies": { "body-parser": "^1.18.3" ...

  6. Asp.Net WebApi 启用CORS跨域访问指定多个域名

    1.后台action指定 EnableCors指定可访问的域名多个,使用逗号隔开 //支持客户端凭据提交,指定多个域名,使用逗号隔开 [EnableCors("http://localhos ...

  7. Asp.Net WebApi+Microsoft.AspNet.WebApi.Core 启用CORS跨域访问

    WebApi中启用CORS跨域访问 1.安装 Nugget包Microsoft.AspNet.WebApi.Cors This package contains the components to e ...

  8. SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问

    SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问 https://blog.csdn.net/yft_android/article/details/80307672

  9. Springboot CORS跨域访问

    Springboot CORS跨域访问 什么是跨域 浏览器的同源策略限制: 它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础 ...

随机推荐

  1. Centos 6.5开启rsync同步

    一.测试环境 操作系统:Centos6.5 Server1:172.18.11.100       源服务器 Server2:172.18.11.110       目标服务器 二.操作步骤: 1.先 ...

  2. 微信小程序+OLAMI(欧拉蜜)自然语言API接口制作智能查询工具--快递、聊天、日历等

    微信小程序最近比较热门,再加上自然语义理解也越来越被人关注,于是我想赶赶潮流,做一个小程序试试.想来想去快递查询应该是一种比较普遍的需求. 如果你也在通过自然语言接口做点什么,希望我的这篇博客能帮到你 ...

  3. ZigZag - 曲折字符串

    需求:将所给的字符串以“倒N型”输出,可以指定输出的行数函数 String convert(String s, int numRows)例如输入“abcdefghijklnmopqrstuvwxyz” ...

  4. Groovy - 介绍

    Groovy 是 用于Java虚拟机的一种敏捷的动态语言,它是一种成熟的面向对象编程语言,既可以用于面向对象编程,又可以用作纯粹的脚本语言.使用该种语言不必编写过多的代码,同时又具有闭包和动态语言中的 ...

  5. Sqoop将mysql数据导入hbase的血与泪

    Sqoop将mysql数据导入hbase的血与泪(整整搞了大半天)  版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: https://my.oschina.net/yunsh ...

  6. java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I

    今天使用hql语句的时候,遇到了一个这样的bug,修改的方法是

  7. Qt之对话框消失动画

    一.效果展示 最近做了一个提示框消失的功能,觉着挺有意思,以前一直以为Qt子窗口不能做淡出效果,其实Qt的淡出功能已经帮我们封装好了,我们仅仅只需要几行代码就可以做出酷炫的窗口关闭效果,写此篇文章的时 ...

  8. hdu 6059---Kanade's trio(字典树)

    题目链接 Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) sat ...

  9. 红帽 Red Hat Linux相关产品iso镜像下载【百度云】(转载)

    不为什么,就为了方便搜索,特把红帽EL 5.EL6.EL7 的各版本整理一下,共享出来.正式发布 6.9 :RedHat Enterprise Server 6.9 for x86_64:rhel-s ...

  10. The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)

    package comxunfang.button; import android.support.v7.app.ActionBarActivity; import android.os.Bundle ...