I'm trying to create an application using NextJS + Java with Spring as my backend and I have been trying the new Spring Authorization Server alongside a BFF app with Spring Gateway and Spring Security Oauth2 Client.
I have recently been studying this sample to try to implement a Backend for Frontend pattern for my own app and I got into a few questions around the Spring Security Oauth2 Client.
I have read this and it appears that it indeed handles the authorization code, refresh token, client credentials automatically, as shown in the following piece of code, but my question is if it really handles everything by it's own specially refreshing the token and such, I've read a about it but it's my first time around Oauth2 Client and I wanted to be really sure about it or if I'm completely lost.
@Bean @Primary // Needed because of GatewayReactiveOAuth2AutoConfiguration public ReactiveOAuth2AuthorizedClientManager authorizedClientManager( ReactiveClientRegistrationRepository clientRegistrationRepository, ServerOAuth2AuthorizedClientRepository authorizedClientRepository) { ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder() .authorizationCode() .refreshToken() .clientCredentials() .build(); DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager = new DefaultReactiveOAuth2AuthorizedClientManager( clientRegistrationRepository, authorizedClientRepository); authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); return authorizedClientManager;
}
The second question is, since the Spring Authorization Server saves the RegisteredClient, Authorization and AuthorizationConsent, should the Backend For Frontend application store the tokens in database somehow? I see those repositories from the code from above and I don't know if that's needed to implement and save. I'm confused about how it works in a production environment if I got to spun up multiple pods with the BFF or the Auth Server and how to handle the session between my frontend and the BFF correctly when dealing with multiple BFF instances.