spring could 微服务 跨域问题(CORS )
问题发现
正常情况下,跨域是这样的:
1. 微服务配置跨域+zuul不配置=有跨域问题
2. 微服务配置+zuul配置=有跨域问题
3. 微服务不配置+zuul不配置=有跨域问题
4. 微服务不配置+zuul配置=ok
然而云环境中每个服务自己有跨域解决方案,而网关需要做最外层的跨域解决方案.如果服务已有跨域配置网关也有,会出现*多次配置问题。
Access-Control-Allow-Origin:"*,*"
也就是multiple Access-Control-Allow-Origin
!!!所以我们就要,微服务配置+zuul配置=解决跨域问题
zuul的跨域忽略配置
使用ZUUL配置忽略头部信息
zuul:
#需要忽略的头部信息,不在传播到其他服务
sensitive-headers: Access-Control-Allow-Credentials,Access-Control-Allow-Origin,Access-Control-Allow-Methods
ignored-headers: Access-Control-Allow-Credentials,Access-Control-Allow-Origin, Access-Control-Allow-Methods,H-APP-Id,Token,APPToken
微服务应用的跨域配置
@Slf4j
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
// 允许任何域名使用
corsConfiguration.addAllowedOrigin("*");
// 允许任何头
corsConfiguration.addAllowedHeader("*");
// 允许任何方法(post、get等)
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
} @Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// 对接口配置跨域设置
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
} }
如果上面不行再配置文件中再加上这
spring:
aop:
auto: true
proxy-target-class: true
application:
name: service-zuul
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedHeaders: '*'
allowedMethods: '*'
allowedOrigins: '*'
只要网关配置就好 , 底下的服务不需要配置。
两个同时配置还导致了 跨域失败 适得其反。
关于zuul 的配置 github上有 https://github.com/wangbensen/common-parent.git
spring could 微服务 跨域问题(CORS )的更多相关文章
- springcloud中通过Filter实现微服务跨域访问允许
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.an ...
- ABP框架中微服务跨域调用其它服务接口
AjaxResponse为ABP自动包装的JSON格式 /// <summary> /// 通过地址和参数取得返回OutPut数据 /// </summary> /// < ...
- 跨域资源共享CORS与JSONP
同源策略限制: 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果没有同源策略,攻击者可以通过JavaScript获取你的邮件以及其他敏感信息,比如说 ...
- 一张图了解Spring Cloud微服务架构
Spring Cloud作为当下主流的微服务框架,可以让我们更简单快捷地实现微服务架构.Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟.经得起实际考验的服务框架组合起来 ...
- Spring Cloud微服务下的权限架构调研
随着微服务架构的流行,系统架构调整,项目权限系统模块开发提上日程,需要对权限架构进行设计以及技术选型.所以这段时间看了下相关的资料,做了几个对比选择. 一.架构图 初步设想的架构如下,结构很简单:eu ...
- Spring Cloud微服务学习笔记
Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...
- 跨域资源共享(CORS)问题解决方案
CORS:Cross-Origin Resource Sharing(跨域资源共享) CORS被浏览器支持的版本情况如下:Chrome 3+.IE 8+.Firefox 3.5+.Opera 12+. ...
- asp.net core webapi之跨域(Cors)访问
这里说的跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被当作 ...
- rest_framework 跨域和CORS
跨域和CORS 本节目录 一 跨域 二 CORS 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 跨域 同源策略(Same origin policy)是一种约定, ...
随机推荐
- JAVA笔记整理(十),JAVA中的File
File类提供对针对目录和文件的读写改等一系列操作方法 创建: public class FileDemo01 { public static void main(String[] args) { t ...
- python之迭代器、生成器及列表推导式
一.迭代器 迭代器就是迭代的工具,迭代是一个重复的过程,每次重复都是一次迭代并且每次迭代的结果都是下次迭代的初始值. lst=[1,2,3,4,5] count=0 while count<le ...
- Flutter——Checkbox组件、CheckboxListTile(多选框组件)
Checkbox组件 Checkbox组件常用的属性: 属性 描述 value true 或者 false onChanged 改变的时候触发的事件 activeColor 选中的颜色.背景颜色 c ...
- Django modle基础样版
定义一个基类模版, from django.db import models class ModelBase(models.Model): """ "" ...
- Eclipse启动tomcat超时
启动tomcat 超时 Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the ser ...
- OpenLayer3入门——[一]
一.OpenLayer3下载 首先下载OpenLayer3开发包,步骤如下: 下载地址https://github.com/openlayers/openlayers/releases/tag/v3. ...
- concat以及group_concat的用法
concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null ...
- LightOJ-1010-Knights in Chessboard(数学)
链接: https://vjudge.net/problem/LightOJ-1010 题意: Given an m x n chessboard where you want to place ch ...
- [CSS] The :empty Pseudo Selector Gotchas
The :empty pseudo selector selects empty elements. We can use this to display useful messages instea ...
- centos svn的安装和配置
1.安装svn yum -y install subversion 2.配置 mkdir /home/svn/admin/test mkdir /home/svn/svnrepos svnadmin ...