How to setup reactive client with OAuth in Spring Boot?
I began a fresh project with WebFlux and aimed to access an API using OAuth2 authorization. While the official documentation provided some guidance on setting up OAuth2 with WebFlux, it didn’t fully address my specific scenario. Consequently, I turned to Baeldung’s article titled Spring Security OAuth Login with WebFlux
for additional insights. However, upon implementing the solution outlined in the article, I encountered an exception—specifically, the UnAuthenticatedServerOAuth2AuthorizedClientRepository
exception—upon starting the application.
Following that, proceed to include configuration details in the src/main/resources/application.yml
file. In this instance, your client will be identified by the key “pet-finder.” This key serves as a registration ID and will be essential for subsequent steps. It’s worth noting that you can accommodate multiple OAuth configurations, and these registration ID’s will serve to differentiate them.
Next, establish configuration with a WebClient bean. In order to authenticate with the Petfinder API, the WebClient must be augmented with a filter – ServerOAuth2AuthorizedClientExchangeFilterFunction
. Employing the registration ID “pet-finder” established earlier, configure the token URI and authorization grant type accordingly.
To properly configure your client ID and secret for your Spring application, it’s essential not to embed them directly into your source code. Storing secrets within the code itself can lead to security risks. Instead, it’s recommended to pass them as arguments when starting your Spring application. When using Maven, you can achieve this with the following command:
Now, with this setup, you’re ready to utilize the WebClient to retrieve details about pets awaiting adoption.
My site is free of ads and trackers. Was this post helpful to you? Why not
Reference:
Disqus is great for comments/feedback but I had no idea it came with these gaudy ads.