[英]Spring WebClient with Custom OAuth2 request
我正在構建一個應用程序,該應用程序必須與使用 OAuth2 和授權類型 client_credentials 保護的 REST 服務進行通信,問題是 /oauth/token 端點需要自定義標頭,為了簡化起見,我們將其稱為“自定義標頭” ”。 我的問題是文檔中沒有關於如何完成此操作的示例或跟蹤。
我的代碼如下
ClientRegistration client = ClientRegistration
.withRegistrationId(authUser)
.tokenUri(authUrl)
.clientId(authUser)
.clientSecret(authPassword)
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.scope(authScope)
.build();
ReactiveClientRegistrationRepository clientRegistrations =
new InMemoryReactiveClientRegistrationRepository(client);
ServerOAuth2AuthorizedClientExchangeFilterFunction oauthFilter =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(
clientRegistrations,
new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
oauthFilter.setDefaultClientRegistrationId(authUser);
this.webClient = WebClient.builder()
.filter(oauthFilter)
.defaultHeaders(httpHeaders -> {
httpHeaders.add(CUSTOM_HEADER, customHeader);;
})
.build();
如您所見,我在 WebClient 中設置了 custome 標頭,但它沒有到達 oauth 過濾器。
任何幫助將不勝感激,因為我已經來回兩天了。
最后我的解決方案是重新實現 OAuth2ExchangeFilterFunction 並實現我自己的 CustomWebClientCredentialsTokenResponseClient
在 CustomWebClientCredentialsTokenResponseClient 中,我添加了提供者期望的自定義標頭,並且在 createDefaultAuthorizedClientManager() 方法中,我必須創建我創建的 CustomWebClientCredentialsTokenResponseClient 的實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.