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方法任意一个不同就 ...
随机推荐
- 异步机制 - IO完成端口
1 KQUEUE KeInitializeQueue VOID KeInitializeQueue( IN PKQUEUE Queue, IN ULONG Count OPTIONAL ); l ...
- pwn学习日记Day7 基础知识积累
知识杂项 strncpy(char s1,const char s2,int n); 其中有三个参数分别表示目标字符串s1,源字符串s2,拷贝长度.意思是将s2指向的字符串的前n个长度的字符放到s1指 ...
- Angular4.x+Ionic3 踩坑之路之打包时出现JAVASCRIPT HEAP OUT OF MEMORY的几种解决办法
最近开发的一个比较大型的App时打生产环境包是出现内存不足导致打包失败的问题.然后百度发现解决方法都是一样,自己试了一下都没什么暖用,心里只想用呵呵来形容了.最后经朋友介绍,技术问题还得去谷歌,git ...
- mysql中的union操作(整理)
mysql中的union操作(整理) 一.总结 一句话总结: union两侧的字段数和字段类型要是一样的 union可以接多个 orderby和排序可以在最后的union组合之后 1.union简单实 ...
- x86 linux下如何交叉编译?
答: 需要首先指定两个环境变量CROSS_COMPILE和ARCH 如交叉编译arm64的程序: export CROSS_COMPILE="aarch64-linux-gnu-" ...
- 开发软件-IntelliJ IDEA:百科
ylbtech-开发软件-IntelliJ IDEA:百科 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智 ...
- [Java读书笔记] Effective Java(Third Edition) 第 4 章 类和接口
第 15 条: 使类和成员的可访问性最小化 软件设计基本原则:信息隐藏和封装. 信息隐藏可以有效解耦,使组件可以独立地开发.测试.优化.使用和修改. 经验法则:尽可能地使每个类或者成员不被外界访问 ...
- Goland 激活码
实测有效,分享下 Goland
- React Native使用NetInfo对当前系统网络的判断
有网状态: 断网状态: 代码如下: 注意:第一次参考了http://www.hangge.com/blog/cache/detail_1614.html代码,一直显示的是unknow状态... 最后处 ...
- 一条语句kill 多条mysql语句
If information_schema.processlist doesn’t exist on your version of MySQL, this works in a linux scri ...