proxy: auth: routes: customers: oauth2 stores: passthru recommendations: none
您能夠經過proxy.auth.*
設置控制@EnableZuulProxy
下游的受權行爲。例:html
proxy: auth: routes: customers: oauth2 stores: passthru recommendations: none
在此示例中,「客戶」服務獲取OAuth2令牌中繼,「存儲」服務獲取傳遞(受權頭只是經過下游),「建議」服務已刪除其受權頭。若是有令牌可用,則默認行爲是執行令牌中繼,不然爲passthru。java
有關詳細信息,請參閱 ProxyAuthenticationProperties。git
Cloudfoundry的Spring Cloud能夠輕鬆地在Cloud Foundry(平臺即服務)中運行 Spring Cloud應用程序 。Cloud Foundry有一個「服務」的概念,它是「綁定」到應用程序的中間件,本質上爲其提供包含憑據的環境變量(例如,用於服務的位置和用戶名)。github
spring-cloud-cloudfoundry-web
項目爲Cloud Foundry中的webapps的一些加強功能提供基本支持:自動綁定到單點登陸服務,並可選擇啓用粘性路由進行發現。web
spring-cloud-cloudfoundry-discovery
項目提供Spring Cloud Commons DiscoveryClient
的實施,所以您能夠@EnableDiscoveryClient
並將您的憑據提供爲spring.cloud.cloudfoundry.discovery.[email,password]
,而後直接或經過LoadBalancerClient
使用DiscoveryClient
/}(若是您沒有鏈接到Pivotal Web Services,則也爲*.url
)。spring
第一次使用它時,發現客戶端可能很慢,由於它必須從Cloud Foundry獲取訪問令牌。json
如下是Cloud Foundry發現的Spring Cloud應用程序:緩存
@Grab('org.springframework.cloud:spring-cloud-cloudfoundry') @RestController @EnableDiscoveryClient class Application { @Autowired DiscoveryClient client @RequestMapping('/') String home() { 'Hello from ' + client.getLocalServiceInstance() } }
若是您運行它沒有任何服務綁定:服務器
$ spring jar app.jar app.groovy $ cf push -p app.jar
它將在主頁中顯示其應用程序名稱。架構
DiscoveryClient
能夠根據身份驗證的憑據列出空間中的全部應用程序,其中的空間默認爲客戶端運行的空間(若是有的話)。若是組織和空間都不配置,則它們將根據Cloud Foundry中的用戶配置文件進行默認。
注意
|
全部OAuth2 SSO和資源服務器功能在版本1.3中移動到Spring Boot。您能夠在Spring Boot用戶指南中找到文檔。 |
該項目提供從CloudFoundry服務憑據到Spring Boot功能的自動綁定。若是您有一個稱爲「sso」的CloudFoundry服務,例如,使用包含「client_id」,「client_secret」和「auth_domain」的憑據,它將自動綁定到您使用@EnableOAuth2Sso
啓用的Spring OAuth2客戶端來自Spring Boot)。可使用spring.oauth2.sso.serviceId
對服務的名稱進行參數化。
文獻做者:Adam Dudczak,MathiasDüsterhöft,Marcin Grzejszczak,Dennis Kieselhorst,JakubKubryński,Karol Lassak,Olga Maciaszek-Sharma,MariuszSmykuła,Dave Syer
Dalston.RELEASE
您始終須要的是將新功能推向分佈式系統中的新應用程序或服務的信心。該項目爲Spring應用程序中的消費者驅動Contracts和服務架構提供支持,涵蓋了一系列用於編寫測試的選項,將其做爲資產發佈,聲稱生產者和消費者保留合同用於HTTP和消息的交互。
重要
|
Spring Cloud發佈列表BOM導入spring-cloud-contract-dependencies ,這反過來又排除了WireMock所需的依賴關係。這可能致使一種狀況,即便你不使用Spring Cloud Contract,那麼你的依賴將會受到影響。 |
若是您有一個使用Tomcat做爲嵌入式服務器的Spring Boot應用程序(默認爲spring-boot-starter-web
)),那麼您能夠簡單地將spring-cloud-contract-wiremock
添加到類路徑中並添加@AutoConfigureWireMock
,以即可以在測試中使用Wiremock。Wiremock做爲存根服務器運行,您可使用Java API或經過靜態JSON聲明來註冊存根行爲,做爲測試的一部分。這是一個簡單的例子:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @AutoConfigureWireMock(port = 0) public class WiremockForDocsTests { // A service that calls out over HTTP @Autowired private Service service; // Using the WireMock APIs in the normal way: @Test public void contextLoads() throws Exception { // Stubbing WireMock stubFor(get(urlEqualTo("/resource")) .willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("Hello World!"))); // We're asserting if WireMock responded properly assertThat(this.service.go()).isEqualTo("Hello World!"); } }
要使用@AutoConfigureWireMock(port=9999)
(例如)啓動不一樣端口上的存根服務器,而且對於隨機端口使用值0.存根服務器端口將在測試應用程序上下文中綁定爲「wiremock.server.port」。使用@AutoConfigureWireMock
將一個類型爲WiremockConfiguration
的bean添加到測試應用程序上下文中,它將被緩存在具備相同上下文的方法和類之間,就像通常的Spring集成測試同樣。
若是您使用@AutoConfigureWireMock
,則它將從文件系統或類路徑註冊WireMock JSON存根,默認狀況下爲file:src/test/resources/mappings
。您可使用註釋中的stubs
屬性自定義位置,這能夠是資源模式(ant-style)或目錄,在這種狀況下,附加*/.json
。例:
@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureWireMock(stubs="classpath:/stubs") public class WiremockImportApplicationTests { @Autowired private Service service; @Test public void contextLoads() throws Exception { assertThat(this.service.go()).isEqualTo("Hello World!"); } }
注意
|
實際上,WireMock老是從src/test/resources/mappings 中加載映射以及 stubs屬性中的自定義位置。要更改此行爲,您還必須以下所述指定文件根。 |
WireMock能夠從類路徑或文件系統上的文件讀取響應體。在這種狀況下,您將在JSON DSL中看到響應具備「bodyFileName」而不是(文字)「body」。默認狀況下,相對於根目錄src/test/resources/__files
解析文件。要自定義此位置,您能夠將@AutoConfigureWireMock
註釋中的files
屬性設置爲父目錄的位置(即,位置__files
是子目錄)。您可使用Spring資源符號來引用file:…
或classpath:…
位置(但不支持通用URL)。能夠給出值列表,而且WireMock將在須要查找響應體時解析存在的第一個文件。
注意
|
當配置files 根時,它會影響自動加載存根(它們來自稱爲「映射」的子目錄中的根位置)。files 的值對從stubs 屬性明確加載的存根沒有影響。 |
對於更常規的WireMock體驗,使用JUnit @Rules
啓動和中止服務器,只需使用WireMockSpring
便利類來獲取Options
實例:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class WiremockForDocsClassRuleTests { // Start WireMock on some dynamic port // for some reason `dynamicPort()` is not working properly @ClassRule public static WireMockClassRule wiremock = new WireMockClassRule( WireMockSpring.options().dynamicPort()); // A service that calls out over HTTP to localhost:${wiremock.port} @Autowired private Service service; // Using the WireMock APIs in the normal way: @Test public void contextLoads() throws Exception { // Stubbing WireMock wiremock.stubFor(get(urlEqualTo("/resource")) .willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("Hello World!"))); // We're asserting if WireMock responded properly assertThat(this.service.go()).isEqualTo("Hello World!"); } }
使用@ClassRule
表示服務器將在此類中的全部方法後關閉。