spring-boot-plus CORS跨域处理
CORS跨域处理
CORS:Cross-Origin Resource Sharing
- CORS是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。
处理方法
- 后台设置允许的请求源/请求头等信息
后台配置
CorsFilter Bean配置
使用
Spring提供的CorsFilter过滤器实现跨域配置
io.geekidea.springbootplus.core.config.SpringBootPlusCorsConfig
/**
* CORS跨域设置
*
* @return
*/
@Bean
public FilterRegistrationBean corsFilter(SpringBootPlusCorsProperties corsProperties) {
log.debug("corsProperties:{}", corsProperties);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 跨域配置
corsConfiguration.setAllowedOrigins(corsProperties.getAllowedOrigins());
corsConfiguration.setAllowedHeaders(corsProperties.getAllowedHeaders());
corsConfiguration.setAllowedMethods(corsProperties.getAllowedMethods());
corsConfiguration.setAllowCredentials(corsProperties.isAllowCredentials());
corsConfiguration.setExposedHeaders(corsProperties.getExposedHeaders());
corsConfiguration.setMaxAge(corsConfiguration.getMaxAge());
source.registerCorsConfiguration(corsProperties.getPath(), corsConfiguration);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
bean.setEnabled(corsProperties.isEnable());
return bean;
}
配置文件
配置文件类:
io.geekidea.springbootplus.core.properties.SpringBootPlusCorsProperties
- application.yml
spring-boot-plus:
############################ CORS start ############################
# CORS跨域配置,默认允许跨域
cors:
# 是否启用跨域,默认启用
enable: true
# CORS过滤的路径,默认:/**
path: /**
# 允许访问的源
allowed-origins: '*'
# 允许访问的请求头
allowed-headers: x-requested-with,content-type,token
# 是否允许发送cookie
allow-credentials: true
# 允许访问的请求方式
allowed-methods: OPTION,GET,POST
# 允许响应的头
exposed-headers: token
# 该响应的有效时间默认为30分钟,在有效时间内,浏览器无须为同一请求再次发起预检请求
max-age: 1800
############################ CORS end ##############################
参考
GITHUB:https://github.com/geekidea/spring-boot-plus
spring-boot-plus CORS跨域处理的更多相关文章
- spring boot:解决cors跨域问题的两种方法(spring boot 2.3.2)
一,什么是CORS? 1,CORS(跨域资源共享)(CORS,Cross-origin resource sharing), 它是一个 W3C 标准中浏览器技术的规范, 它允许浏览器向非同一个域的服务 ...
- Spring MVC学习总结(10)——Spring MVC使用Cors跨域
跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP 请求.比如说,域名A(http://domaina.examp ...
- Java Spring boot 2.0 跨域问题
跨域 一个资源会发起一个跨域HTTP请求(Cross-site HTTP request), 当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时 . 比如说,域名A(http://dom ...
- spring boot项目配置跨域
1.在项目启动入口类实现 WebMvcConfigurer 接口: @SpringBootApplication public class Application implements WebMvcC ...
- spring boot之配置跨域
在启动类中配置 @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override p ...
- 关于 Spring Security OAuth2 中 CORS 跨域问题
CORS 是一个 W3C 标准,全称是”跨域资源共享”(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了 AJA ...
- spring boot / cloud (六) 开启CORS跨域访问
spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...
- Spring Boot Web应用开发 CORS 跨域请求支持:
Spring Boot Web应用开发 CORS 跨域请求支持: 一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等CORS与JSONP相比 1. JSONP只能实现 ...
- Spring Boot 2中对于CORS跨域访问的快速支持
原文:https://www.jianshu.com/p/840b4f83c3b5 目前的程序开发,大部分都采用前后台分离.这样一来,就都会碰到跨域资源共享CORS的问题.Spring Boot 2 ...
- spring MVC cors跨域实现源码解析
# spring MVC cors跨域实现源码解析 > 名词解释:跨域资源共享(Cross-Origin Resource Sharing) 简单说就是只要协议.IP.http方法任意一个不同就 ...
随机推荐
- 蜗牛圈圈-时尚智能的运动计时App
Duang! 各类运动爱好者的福音来啦! 蜗牛圈圈-最智能的圈速计时助手 扫描二维码下载体验 [产品简介] -蜗牛圈圈是一款专业的圈速计时工具,帮助您获得整个运动过程中的各项数据,保存记录,分享激情. ...
- OpenResty之ngx_lua模块的加密接口
原文: ngx_Lua模块中的加密api接口 ngx.crc32_short digest = ngx.crc32_short(str) 该方法主要是计算给定字符串 str 的循环校验码(Cyclic ...
- 【Linux】安装 node.js
1.在 linux 上安装 node.js 有几种方式,这里 教的是最简单的一种,因为其他都比较坑. 2.先去 node.js 官网 获取到已经编译好的安装包的地址, 3.使用 wget 去下载上面获 ...
- Resend a Request by fiddler
Resend a Request You can resend a request directly from the Sessions List, or save requests to resen ...
- centos7 设置 防火墙 开机自启
CentOS 7.0默认使用的是firewall作为防火墙,之前版本是使用iptables. 1.设置firewall开机启动 systemctl enable firewalld 2.禁止firew ...
- ybatis 逆向工程 自动生成的mapper文件没有 主键方法
1.数据表没有设置主键 设置个主键就好 2.在mybits配置文档里设置了某些属性值为false 在mybatis配置文档里查看 enableSelectByPrimaryKey="true ...
- JS判断与过滤的表情符号表情的方法
一.js判断文本中是否有表情符号表情 isEmojiCharacter(substring){ for ( var i = 0; i <substring.length; i ++){ var ...
- https://uwsgi-docs.readthedocs.io/en/latest/Async.html
Beware! Async modes will not speed up your app, they are aimed at improving concurrency. Do not expe ...
- Linux-命令与文件的查询
命令与文件的查询: 1.脚本文件名的查询: which(寻找执行文件) 命令格式: which [-a] command -a:列出查询到的所有命令的路径 2.文件名的查找: whereis.loca ...
- wangEditor编辑器控件里textarea的id不要用content
头引用 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></scri ...