79158407

Date: 2024-11-05 09:18:54
Score: 0.5
Natty:
Report link

My working implementation boils down to two things:

  1. Set clockSkew to 0 s.
OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
                .clientCredentials(
                        clientCredentialsGrantBuilder -> clientCredentialsGrantBuilder.clockSkew(Duration.ZERO))
                .build();
  1. Add retry mechanism in case of 401:
public class WebClientRetryHelper {

    public static Retry retryUnauthorized() {
        return Retry.fixedDelay(1, Duration.ofMillis(500))
                .filter(throwable -> {
                    if (throwable instanceof WebClientResponseException ex) {
                        return ex.getStatusCode() == HttpStatus.UNAUTHORIZED;
                    }
                    return false;
                });
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: dan