content: package ag.selm.catalogue.config;
  
  import de.codecentric.boot.admin.client.registration.BlockingRegistrationClient;
  import de.codecentric.boot.admin.client.registration.RegistrationClient;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  import org.springframework.boot.web.client.RestTemplateBuilder;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpHeaders;
  import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
  import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
  import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
  import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
  import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
  import org.springframework.web.client.RestTemplate;
  
  @Configuration
  public class ClientBeans {
  
      @Bean
      @ConditionalOnProperty(name = "spring.boot.admin.client.enabled", havingValue = "true")
      public RegistrationClient registrationClient(
              ClientRegistrationRepository clientRegistrationRepository,
              OAuth2AuthorizedClientService authorizedClientService
      ) {
          AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager =
                  new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository,
                          authorizedClientService);
  
          RestTemplate restTemplate = new RestTemplateBuilder()
                  .interceptors((request, body, execution) -> {
                      if (!request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
                          OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(OAuth2AuthorizeRequest
                                  .withClientRegistrationId("keycloak")
                                  .principal("catalogue-service-metrics-client")
                                  .build());
  
                          request.getHeaders().setBearerAuth(authorizedClient.getAccessToken().getTokenValue());
                      }
  
                      return execution.execute(request, body);
                  })
                  .build();
          return new BlockingRegistrationClient(restTemplate);
      }
  }
  