Skip to content

Refreshing expired OAuth token leads to failing requests with ServerOAuth2AuthorizedClientExchangeFilterFunction using RemoveAuthorizedClientReactiveOAuth2AuthorizationFailureHandler #9094

@chriskn

Description

@chriskn

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()
}

Metadata

Metadata

Assignees

Labels

in: oauth2An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose)status: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions