@@ -126,6 +126,39 @@ void restTemplateAddsBearerTokenFromKeycloakUsingClientCredentials() {
126126 assertThat (authHeader ).isNotNull ().startsWith ("Bearer " );
127127 }
128128
129+ @ Test
130+ void restTemplateAddsBearerTokenFromKeycloakUsingClientCredentialsAndIssuerUri () {
131+ // given OAuth2 client configuration pointing to Keycloak issuer endpoint
132+ String issuerUri = keycloak .getAuthServerUrl () + "/realms/test-realm" ;
133+
134+ ConfigClientProperties props = new ConfigClientProperties ();
135+ props .getOauth2 ().setEnabled (true );
136+
137+ OAuth2ClientProperties .Provider provider = props .getOauth2 ().getProvider ();
138+ provider .setIssuerUri (issuerUri );
139+
140+ OAuth2ClientProperties .Registration registration = props .getOauth2 ().getRegistration ();
141+ registration .setClientId ("config-client" );
142+ registration .setClientSecret ("my-client-secret" );
143+ registration .setAuthorizationGrantType ("client_credentials" );
144+
145+ ConfigClientRequestTemplateFactory factory = new ConfigClientRequestTemplateFactory (log , props );
146+ RestTemplate restTemplate = factory .create ();
147+
148+ // when
149+ String url = "http://localhost:" + mockServer .getLocalPort () + "/secure" ;
150+ ResponseEntity <String > response = restTemplate .getForEntity (URI .create (url ), String .class );
151+
152+ // then
153+ assertThat (response .getStatusCode ().is2xxSuccessful ()).isTrue ();
154+ assertThat (response .getBody ()).isEqualTo ("ok" );
155+
156+ HttpRequest [] recorded = mockClient .retrieveRecordedRequests (request ().withPath ("/secure" ));
157+ assertThat (recorded ).hasSize (1 );
158+ String authHeader = recorded [0 ].getFirstHeader ("Authorization" );
159+ assertThat (authHeader ).isNotNull ().startsWith ("Bearer " );
160+ }
161+
129162 @ Test
130163 void restTemplateDoesNotAddAuthorizationHeaderWhenOauth2Disabled () {
131164 // given ConfigClientProperties with OAuth2 disabled
0 commit comments