Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebClient with OAuth2 authorization

When integrating with some API behind OAuth authorization using old Spring's RestTemplate I was doing some kind of:

ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();resourceDetails.setClientId(oauthClientId);
resourceDetails.setClientSecret(oauthClientSecret);
resourceDetails.setAccessTokenUri(accessTokenUri);
// and we got the restTemplate:
OAuth2RestTemplate client = new OAuth2RestTemplate(resourceDetails);

and then GET goes like:

client.getForEntity(restApiUri, MyEntity.class);

But it is blocking :(

As a workaround I wrapped old RestTempate using Mono.fromCallable(...). I tried also do some custom client using WebClient that takes token and authorize user and then do second call to RestApi. But it is not convinient as token can expire and I would have to implement renewal process. There can also be more pitfalls in OAuth that I'm not aware of.

How I can do it using Spring web-flux and WebClient with Mono/Flux reactive objects?

like image 862
LukaszS Avatar asked Nov 08 '22 13:11

LukaszS


1 Answers

Maybe this will help you. Medium article about configuring the webclient to use it the way you mentioned.

like image 105
Asce4s Avatar answered Nov 14 '22 23:11

Asce4s