Our application supports federation. So we will have to generate token from authentication server located in multiple regions. Every region has a different endpoint. With that in place we are looking for a way to switch the token URI in the runtime based on the authorization code value.
In the current implementation we see that client registration happens at the start of the application and details are saved in the context and there is no way to alter any of the values in the runtime.
Can you let us know if there is a possible way to alter the tokenURI for each access token request?
Comment From: sjohnr
Thanks for getting in touch @pradeep-kodavoor, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add a minimal sample that reproduces this issue if you feel this is a genuine bug.
Having said that, OAuth2 Client features are designed to support a number of customizations using delegation. You can provide an OAuth2AccessTokenResponseClient that can override the tokenUri like this:
@Bean
public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() {
DefaultAuthorizationCodeTokenResponseClient delegate = new DefaultAuthorizationCodeTokenResponseClient();
return (grantRequest) -> {
ClientRegistration clientRegistration =
ClientRegistration.withClientRegistration(grantRequest.getClientRegistration())
.tokenUri("...")
.build();
OAuth2AuthorizationCodeGrantRequest updatedGrantRequest =
new OAuth2AuthorizationCodeGrantRequest(
clientRegistration, grantRequest.getAuthorizationExchange());
return delegate.getTokenResponse(updatedGrantRequest);
};
}