Source:
<code>public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String registrationId, Authentication authentication, HttpServletRequest request) { try { ClientRegistration clientRegistration = azureClientRegistrationRepository .findByRegistrationId(registrationId); AbstractOAuth2TokenAuthenticationToken<AbstractOAuth2Token> authenticationToken = (AbstractOAuth2TokenAuthenticationToken) authentication; String accessToken = authenticationToken.getToken().getTokenValue(); OnBehalfOfParameters parameters = OnBehalfOfParameters .builder(clientRegistration.getScopes(), new UserAssertion(accessToken)) .build(); <START> ConfidentialClientApplication clientApplication = confidentialClientApplicationMap.get(clientRegistration <END> .getClientId()); String oboAccessToken = clientApplication.acquireToken(parameters).get().accessToken(); JWT parser = JWTParser.parse(oboAccessToken); Date iat = (Date) parser.getJWTClaimsSet().getClaim("iat"); Date exp = (Date) parser.getJWTClaimsSet().getClaim("exp"); OAuth2AccessToken oAuth2AccessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, oboAccessToken, Instant.ofEpochMilli(iat.getTime()), Instant.ofEpochMilli(exp.getTime())); OAuth2AuthorizedClient oAuth2AuthorizedClient = new OAuth2AuthorizedClient(clientRegistration, authenticationToken.getName(), oAuth2AccessToken); return (T) oAuth2AuthorizedClient; } catch (Throwable throwable) { LOG.error("Failed to loadAuthorizedClient", throwable); } return null; }</code><technical_language>familiar Spring Security - creating a ConfidentialClient application request, is created startup reused</technical_language>

Target:
public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String registrationId, Authentication authentication, HttpServletRequest request) { String oboAuthorizedClientAttributeName = OBO_AUTHORIZEDCLIENT_PREFIX + registrationId; if (request.getAttribute(oboAuthorizedClientAttributeName) != null) { return (T) request.getAttribute(oboAuthorizedClientAttributeName); } if (!(authentication instanceof AbstractOAuth2TokenAuthenticationToken)) { throw new IllegalStateException("Unsupported token implementation " + authentication.getClass()); } try { String accessToken = ((AbstractOAuth2TokenAuthenticationToken<?>) authentication).getToken().getTokenValue(); ClientRegistration clientRegistration = azureClientRegistrationRepository.findByRegistrationId(registrationId); if (clientRegistration == null) { LOGGER.warn("Not found the ClientRegistration."); return null; } OnBehalfOfParameters parameters = OnBehalfOfParameters .builder(clientRegistration.getScopes(), new UserAssertion(accessToken)) .build(); ConfidentialClientApplication clientApplication = getClientApplication(clientRegistration.getRegistrationId()); if (null == clientApplication) { LOGGER.warn("Not found the " + clientRegistration.getRegistrationId() + " ConfidentialClientApplication."); return null; } String oboAccessToken = clientApplication.acquireToken(parameters).get().accessToken(); JWT parser = JWTParser.parse(oboAccessToken); Date iat = (Date) parser.getJWTClaimsSet().getClaim("iat"); Date exp = (Date) parser.getJWTClaimsSet().getClaim("exp"); OAuth2AccessToken oAuth2AccessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, oboAccessToken, Instant.ofEpochMilli(iat.getTime()), Instant.ofEpochMilli(exp.getTime())); OAuth2AuthorizedClient oAuth2AuthorizedClient = new OAuth2AuthorizedClient(clientRegistration, authentication.getName(), oAuth2AccessToken); request.setAttribute(oboAuthorizedClientAttributeName, (T) oAuth2AuthorizedClient); return (T) oAuth2AuthorizedClient; } catch (Throwable throwable) { LOGGER.error("Failed to load authorized client.", throwable); } return null; }