問題一:
spring::
name
: nb-web
cloud
:nacos
:discovery
:server-addr
: localhost:
8848
gateway
:globalcors
:corsconfigurations
: '[/**]':
allowedheaders
:"*"
allowedorigins
:"*"
allowcredentials
:true
allowedmethods
:- get
- post
- delete
- put
- option
第二種再啟動類上新增bean:
/**
* 配置跨域
* @return
*/@bean
public corswebfilter corsfilter()
這樣就能解決自定義headers被攔截和跨域問題,但是這樣配置緊接著下乙個問題又出來了
問題二由於跨域配置會進行疊加,舉個例子你在服務一種新增了跨域配置,在spring cloud gateway中也新增了,那這樣就會產生headers重複的問題:
前端就會報錯,
所以你必須要進行header去重,具體辦法如下:在spring cloud gateway的過濾器中新增過濾配置
spring::
name
: nb-web
cloud
:nacos
:discovery
:server-addr
: localhost:
8848
gateway
:default-filters
:- deduperesponseheader=vary access-control-allow-credentials access-control-allow-origin, retain_unique
- deduperesponseheader=access-control-allow-origin, retain_first
這樣就能把重複的header給去掉了,spring cloud gateway過濾器的詳細內容可以參考:
這樣跨域問題基本就能解決了,但是因為我這邊需要進行session共享,所以需要給前端傳入cookie,那麼問題又來了,後端配置的cookie傳不出去,原因有兩個1:沒有加上headers上面加上access-control-allow-credentials: true,這個可以在
gateway
:default-filters
:- addresponseheader=access-control-allow-credentials,
true
新增上,還有乙個就是前端的ajax上沒有加上:
, xhrfields:
, crossdomain: true
這樣瀏覽器就能儲存cookie
至此問題就能解決了。在查閱過程中我看到關於跨域配置重複問題是存在bug的,具體內容可以參考:
Spring gateway的動態路由
gateway官方文件 gateway是spring cloud中乙個用於替換zuul閘道器作用的子專案,基於webflux實現了非同步非阻塞處理。gateway工程首先需在啟動時注入route,在請求進來時,會根據route的predicate匹配路由規則,然後經過gatewayfilter以及g...
spring gateway閘道器的使用
1建立module檔案 2匯入依賴 org.springframework.cloudgroupid spring cloud starter gatewayartifactid dependency 3配置yml檔案 server port 9527 spring name cloud gatew...
springgateway限流 令牌桶演算法
參見 lua指令碼 參見spring spring cloud gateway core包下的request rate limiter.lua redis從2.6版本開始引入對lua指令碼的支援,通過在伺服器中嵌入lua環境,redis客戶端可以使用lua指令碼,直接在伺服器端原子地執行多個redi...