-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Hi, I am trying to configure a OAuth 2.0 aware WebClient using ServerOAuth2AuthorizedClientExchangeFilterFunction and have issues with expiring tokens.
Spring (security) version: 5.3.4
Current behavior:
If a token expires the RemoveAuthorizedClientReactiveOAuth2AuthorizationFailureHandler deletes the client and an error is returned leading to a failing request. For the following requests a new authenticated client is created.
Expected behavior
If a token expires, the RemoveAuthorizedClientReactiveOAuth2AuthorizationFailureHandler deletes the client and a new authenticated client is created.
This new client is used for the current and the following requests.
The request succeeds with the newly created client and no error is reported.
Question
Is it possible to implement a OAuth filter which handles the creation of a new client in a way that no error occurs and the request succeeds? Before we updated to version 5.3.X this issue did not occur.
Code snipped
I implemented my WebClient bean like this (kotlin):
@Bean(name = ["webclient"])
fun webClient(): WebClient {
val clientRegistryRepo = InMemoryReactiveClientRegistrationRepository(
ClientRegistration
.withRegistrationId("someId")
.tokenUri("someUri")
.clientId("someClientId")
.clientSecret("secret")
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.build()
)
val clientService = InMemoryReactiveOAuth2AuthorizedClientService(clientRegistryRepo)
val authorizedClientManager =
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistryRepo, clientService)
val oauthFilter = ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager)
oauthFilter.setDefaultClientRegistrationId(paymentServiceSecretsResult.value.registrationId)
oauthFilter.setAuthorizationFailureHandler(handleOAuthFailure(clientService))
return WebClient.builder()
.filter(oauthFilter)
.baseUrl("someBaseUrl")
.build()
}
private fun handleOAuthFailure(
clientService: ReactiveOAuth2AuthorizedClientService
) = RemoveAuthorizedClientReactiveOAuth2AuthorizationFailureHandler {clientRegistrationId, principal, _ ->
clientService.removeAuthorizedClient(clientRegistrationId, principal.name).block()
Mono.empty()
}