一、CORS 配置
你可以配置網(wǎng)關(guān)來控制全局或每個路由的 CORS 行為。兩者都提供同樣的可能性。
1. Global CORS 配置
“global” CORS配置是對 Spring Framework CorsConfiguration 的URL模式的映射。下面的例子配置了 CORS。
Example 77. application.yml
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "https://docs.spring.io"
allowedMethods:
- GET
在前面的例子中,對于所有GET請求的路徑,允許來自 docs.spring.io 的請求的CORS請求。
要為未被某些網(wǎng)關(guān)路由謂詞處理的請求提供相同的 CORS 配置,請將 spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping 屬性設(shè)為 true。當你試圖支持 CORS 預(yù)檢請求,而你的路由謂詞因為 HTTP 方法是 options 而不能評估為 true 時,這很有用。
2. 路由的 CORS 配置
“route” configuration 允許將CORS直接應(yīng)用于帶有key CORS 的路由作為元數(shù)據(jù)。像全局配置一樣,這些屬性屬于 Spring Framework CorsConfiguration。
如果路由中沒有 Path 謂詞,則將應(yīng)用 '/**'。
Example 78. application.yml
spring:
cloud:
gateway:
routes:
- id: cors_route
uri: https://example.org
predicates:
- Path=/service/**
metadata:
cors
allowedOrigins: '*'
allowedMethods:
- GET
- POST
allowedHeaders: '*'
maxAge: 30
二、路由元數(shù)據(jù)配置
你可以通過使用元數(shù)據(jù)為每個路由配置額外的參數(shù),如下所示。
Example 73. application.yml
spring:
cloud:
gateway:
routes:
- id: route_with_metadata
uri: https://example.org
metadata:
optionName: "OptionValue"
compositeObject:
name: "value"
iAmNumber: 1
你可以從一個 exchange 所獲取所有的元數(shù)據(jù)屬性,如下所示
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
// get all metadata properties
route.getMetadata();
// get a single metadata property
route.getMetadata(someKey);
三、配置(Configuration)
Spring Cloud Gateway 的配置是由 RouteDefinitionLocator 實例的集合驅(qū)動的。下面的列表顯示了 RouteDefinitionLocator 接口的定義。
Example 71. RouteDefinitionLocator.java
public interface RouteDefinitionLocator {
Flux<RouteDefinition> getRouteDefinitions();
}
默認情況下,PropertiesRouteDefinitionLocator 通過使用Spring Boot的 @ConfigurationProperties 機制加載屬性。
前面的配置例子都使用了一種快捷方式,即使用位置參數(shù)而不是命名參數(shù)。下面的兩個例子是等價的。
Example 72. application.yml
spring:
cloud:
gateway:
routes:
- id: setstatus_route
uri: https://example.org
filters:
- name: SetStatus
args:
status: 401
- id: setstatusshortcut_route
uri: https://example.org
filters:
- SetStatus=401
對于網(wǎng)關(guān)的某些用途來說,屬性已經(jīng)足夠了,但一些生產(chǎn)用例會從外部來源(如數(shù)據(jù)庫)加載配置中受益。未來的里程碑版本將有基于 Spring Data Repository 的 RouteDefinitionLocator 實現(xiàn),如 Redis、MongoDB和Cassandra。
四、TLS 和 SSL
網(wǎng)關(guān)可以通過遵循通常的 Spring server configuration 來監(jiān)聽 HTTPS 請求。下面的例子顯示了如何做到這一點。
Example 67. application.yml
server:
ssl:
enabled: true
key-alias: scg
key-store-password: scg1234
key-store: classpath:scg-keystore.p12
key-store-type: PKCS12
你可以將網(wǎng)關(guān)路由到HTTP和HTTPS后端。如果你要路由到HTTPS后端,你可以通過以下配置將網(wǎng)關(guān)配置為信任所有下游的證書。
Example 68. application.yml
spring:
cloud:
gateway:
httpclient:
ssl:
useInsecureTrustManager: true
使用不安全的 trust manager 不適合于生產(chǎn)。對于生產(chǎn)部署,你可以用一組已知的證書來配置網(wǎng)關(guān),它可以通過以下配置來信任。
Example 69. application.yml
spring:
cloud:
gateway:
httpclient:
ssl:
trustedX509Certificates:
- cert1.pem
- cert2.pem
如果 Spring Cloud Gateway 沒有配置受信任的證書,就會使用默認的 trust store(你可以通過設(shè)置 javax.net.ssl.trustStore 系統(tǒng)屬性來覆蓋它)。
1. TLS 握手
網(wǎng)關(guān)維護著一個client pool,它用來路由到后端。當通過HTTPS進行通信時,客戶端發(fā)起了一個TLS握手。一些 timeout 配置與這個握手相關(guān)。你可以對這些 timeouts 進行配置,如下(默認值)。文章來源:http://www.zghlxwxcb.cn/news/detail-639128.html
Example 70. application.yml文章來源地址http://www.zghlxwxcb.cn/news/detail-639128.html
spring:
cloud:
gateway:
httpclient:
ssl:
handshake-timeout-millis: 10000
close-notify-flush-timeout-millis: 3000
close-notify-read-timeout-millis: 0
到了這里,關(guān)于Spring Cloud Gateway系例—參數(shù)配置(CORS 配置、SSL、元數(shù)據(jù))的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!