Spring Security OAuth2 Client token endpoint timeout and retry

186 Views Asked by At

I have configured a simple reactive application as an oauth2 client.

spring:
  security:
    oauth2:
      client:
        provider:
          idp:
            issuer-uri: some-url
        registration:
          idp:
            client-id: some-client-id
            client-secret: some-client-secret
            redirect-uri: some-redirect-uri
            scope: openid, profile, email

Now when I access any secured endpoint, I am, as expected, redirected to an IDP login page. After entering user credentials I am redirected back to my app with the authorization code. Spring security then takes this code and exchanges it for the access token using the token endpoint. It's a standard authorization code flow.

However, sometimes I get a connection timeout on the token endpoint call. Is there an easy way how to retry this call and possibly set a connection timeout without heavy customizations?

So far, I've found nothing in the latest spring documentation.

1

There are 1 best solutions below

1
Poklakni On

I've found my answer here: https://docs.spring.io/spring-security/reference/reactive/oauth2/client/authorization-grants.html

Section Customizing the WebClient

edit:

    @Bean
    public ReactiveAuthenticationManager authorizationCodeAuthenticationManager() {
        var accessTokenResponseClient = new WebClientReactiveAuthorizationCodeTokenResponseClient();
        accessTokenResponseClient.setWebClient(<your custom webClient with retry config>)

        return new OAuth2AuthorizationCodeReactiveAuthenticationManager(accessTokenResponseClient);
    }