当前位置:网站首页>跨域 CORS/OPTIONS
跨域 CORS/OPTIONS
2022-06-22 18:53:00 【黑色幽默595】
跨域解决
浏览器发起
ajax需要遵守同源策略,协议-ip-端口,都必须一致
允许跨域请求到达网关,网关在请求头中加入Access-Control-Allow-Origin
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*" #允许所有来源访问
allowedMethods: "*" #允许所有请求方式跨域访问
Access-Control-Allow-Origin 请求头能
支持的请求get、head、post三种
支持的携带请求头
Content-Type 的值仅限于下列三者之一:告诉服务器这次请求请求体的内容类型。
text/plain: 提交了大文本multipart/form-data: 文件上传application/x-www-form-urlencoded: 普通的表单提交
除此之外的请求,依旧会被拒绝跨域例如
Content-Type:application/json;charser=utf-8都属于会被拒绝
这类请求在浏览器,发起访问之前,会先发送一个OPTIONS请求去服务器,询问能否支持跨域
解决方式
网关设置
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*" #允许所有来源访问
allowedMethods: "*" #允许所有请求方式跨域访问
allowedHeaders: "*" #允许任意请求头跨域
allowCredentials: true #允许Cookie等一些安全标识头跨域
拦截器全局放行
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
public CorsConfig() {
}
@Bean
public CorsFilter corsFilter() {
// 1. 添加cors配置信息
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
// 设置是否发送cookie信息
config.setAllowCredentials(true);
// 设置允许请求的方式
config.addAllowedMethod("*");
// 设置允许的header
config.addAllowedHeader("*");
// 2. 为url添加映射路径
UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
corsSource.registerCorsConfiguration("/**", config);
// 3. 返回重新定义好的corsSource
return new CorsFilter(corsSource);
}
}
边栏推荐
猜你喜欢
![[compréhension approfondie de la base de connaissances tcaplusdb] déploiement de la version locale de tcaplusdb FAQ](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[compréhension approfondie de la base de connaissances tcaplusdb] déploiement de la version locale de tcaplusdb FAQ

iVX无代码挑战五秒游戏制作

市场开始降温,对NFT 是坏事么?

基于Sentinel的高可用限流系统的Grafana报表展示

【深入理解TcaplusDB技术】入门Tcaplus SQL Driver

IDEA写jsp代码报错,但是正常运行解决
Summary of 2019: 31 is just another start

【深入理解TcaplusDB技术】入门TcaplusDB 问题汇总

数字经济加速落地,能为中小企业带来什么?

如何低成本快速搭建企业知识库?
随机推荐
基于Sentinel的高可用限流系统的Grafana报表展示
510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
[deeply understand tcapulusdb technology] how to initialize and launch tcapulusdb machine
Summary of 2019: 31 is just another start
三维天地助力实验室夯实完整质量体系管理
Which securities firm is better to choose for opening an account in flush? Is it safe to open a mobile account?
MySQL高级(二)
Storage structure of graph (adjacency matrix)
【深入理解TcaplusDB技术】TcaplusDB 表管理——重建表
Merge sort (recursive and iterative Implementation)
Classic interview question: a page from entering URL to rendering process
[deeply understand tcapulusdb technology] tcapulusdb operation and maintenance - daily inspection
[deeply understand tcapulusdb technology] how to start tcapulusdb process
一个支持IPFS的电子邮件——SKIFF
市场开始降温,对NFT 是坏事么?
年中大促 | 集成无忧,超值套餐 6 折起
【深入理解TcaplusDB技术】TcaplusDB 表管理——清理表
元宇宙中的云计算,提升你的数字体验
科技云报道:东数西算不止于“算”,更需“新存储”
Three dimensional world helps the laboratory to consolidate the complete quality system management