I am working on a project where the server provide only an access token without the refresh token for some company needs. Referring to official docs, the following code will automatically refresh the access token when it's expired if a refresh token exists. if not it will retrieve a new one for every request.
Here is the optimal code :
@Bean
WebClient webClient(ClientRegistrationRepository clientRegistrations,
OAuth2AuthorizedClientRepository authorizedClients) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
// (optional) explicitly opt into using the oauth2Login to provide an access token implicitly
// oauth.setDefaultOAuth2AuthorizedClient(true);
// (optional) set a default ClientRegistration.registrationId
// oauth.setDefaultClientRegistrationId("client-registration-id");
return WebClient.builder()
.apply(oauth2.oauth2Configuration())
.build();
}
Here is the code that i am using :
@Configuration
public class OauthEmployeConfig{
/**
** ... String baseUrl, String accessUrl for the access token url
**/
@Bean
public WebClient webClient(UserRegistration userRegistr) {
ClientRequest clientRequest = ClientRequest
.create(HttpMethod.POST, URI.create(accessUrl))
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.headers(headers -> headers.setBasicAuth(userRegistr.getClientId(), userRegistr.getClientSecret()))
.body(BodyInserters.fromFormData("grant_type", userRegistr.getAuthorizGrantType())
.with("scope", userRegistr.getScope().replaceAll(",", "")))
.build();
return WebClient.builder()
.baseUrl(baseUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.filter((request, next) -> next.exchange(clientRequest)
.flatMap(response -> response.body(org.springframework.security.oauth2.core.web.reactive.function.OAuth2BodyExtractors.oauth2AccessTokenResponse()))
.map(accessToken -> accessToken.getAccessToken().getTokenValue())
.map(token -> setBearer(request, token))
.flatMap(next::exchange))
.filter(logRequest())
.filter(handleResponseError())
.build();
}
private ClientRequest setBearer(ClientRequest request, String token) {
return ClientRequest.from(request)
.header("Authorization", "Bearer " + token).build();
}
private static ExchangeFilterFunction handleResponseError() {
return ExchangeFilterFunction.ofResponseProcessor(
response -> response.statusCode().isError()
? response.bodyToMono(String.class)
.flatMap(errorBody -> Mono.error(new RuntimeException(errorBody, response.statusCode())))
: Mono.just(response));
}
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
clientRequest.headers().forEach((name, values) -> values.forEach(value -> LOG.info("{}={}", name, value)));
return Mono.just(clientRequest);
});
}
}
I would like to know if there is an approach to get the access token once and keep it for every request while it's not expire, and when it's the case I get a new one!