跨域解决方案
2023-04-18 14:41:04 时间
问题现象
h5和web分别处于不同的域名
因web nginx并未配置允许h5域名访问的白名单
所以h5访问web的资源就出现了跨域问题
复制代码
跨域原理简介
跨域问题来源于浏览器的同源策略
浏览器为了提高网站的安全性
在发送ajax请求时
只有在当前页面地址与请求地址的协议+域名+端口号相同时才允许访问
否则会被拦截
复制代码
处理方式
- nginx反向代理
- cors(跨域资源共享)
nginx反向代理--对应上图中的nginx跨域配置
nginx配置iframe同源访问
# 只允许iframe过来的请求和当前nginx web服务是同一个域名
add_header X-Frame-Options SAMEORIGIN;
复制代码
nginx 允许指定域名列表访问
## 和server同级别
map $http_origin $cors_list{
default http://xxx-te.test.xxxfintech.com;
"~^http://xxx-te.test.xxxfintech.com" http://xxx-te.test.xxxfintech.com;
"~^http://xxx-h5te.test.xxxfintech.com" http://xxx-h5te.test.xxxfintech.com;
}
## server内
add_header 'Access-Control-Allow-Origin' '$cors_list';
复制代码
cors(跨域资源共享)--对应上图中的网关跨域配置
在spring-cloud-gateway中添加跨域配置类
@Configuration
public class CorsAutoConfiguration {
@Autowired
private GlobalCorsProperties globalCorsProperties;
@Bean
public CorsWebFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
globalCorsProperties.getCorsConfigurations().forEach((path,corsConfiguration)->source.registerCorsConfiguration(path, corsConfiguration));
return new CorsWebFilter(source);
}
}
复制代码
在nacos中配置
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allow-credentials: true
allowed-origins: # 允许访问的域名配置
- "http://xxxx"
- "https://xxxx"
allowed-headers: "*"
allowed-methods: "*"
max-age: 3600
相关文章
- SpringCloud集成Hystrix
- 渗透测试之Vulnhub-DC2
- Flask之session伪造
- SpringCloud集成Gateway
- 渗透测试Vulnhub-DC3
- SpringCloud集成Config分布式配置中心
- 如何理解数字化转型?数字化为核心,智慧数字经营为手段
- 鹤城杯杂项MISC部分WP
- Hadoop初始化不成功,Start-all报错
- PHP json_encode不转义中文
- Chrome默认非安全端口
- linux 查看nginx 安装目录
- 详述 Mac GoLand 安装后打不开(闪退)的解决方法
- RabbitMQ---消息队列---上半部分
- 正交实验法之 Allpairs 电商项目用例设计实战
- RabbitMQ---幂等性,优先级队列,惰性队列
- RabbitMQ---集群,Haproxy+Keepalive 实现高可用负载均衡,Federation Exchange和Federation Queue
- RabbitMQ----小总结
- 从零开始学架构-day02
- 智能安防的发展前景是怎样的?